Page 1 of 1
[SOLVED] - String replacement inside another string
Posted: Fri Jul 29, 2005 2:38 pm
by RobertGonzalez
I'm sure there is a simple answer to this question, but for the life of me I can't seem to figure it out.
I use the the phpBB sessions code for one of my websites. In keeping with the instructions I use the append_sid() function for all of the URL's on the site. My problem comes from the updateable content on the start page. The site admins use a CMS to add fresh content to the home page. This content sometime have links in the content body. How would I go about scanning the content (pulled from the DB and read from an array var called $content['page_content']), finding all hyperlinks in the content that link within the domain, and replacing the hyperlinks with an append_sid($hyperlink)?
As always, your help is much appreciated.
PS I searched in these forums for preg_replace, string replace and a few other search terms. I just don't think I know what to look for in the results. Thanks again for your help.
Posted: Fri Jul 29, 2005 8:16 pm
by timvw
Posted: Sun Jul 31, 2005 9:15 am
by RobertGonzalez
Thanks timVW. But I believe this function replaces all the links in the output with the var=value querystring (I am not certain of this as I have not been able to test it yet). I think I might be able to better ask the question.
How would I go about taking a string, looking for all:
Code: Select all
<a href="e;їSOMEHYPERLINK]"e;>
and replace the
[SOMEHYPERLINK] value as long as that value does NOT contain the "http://" string?
Posted: Sun Jul 31, 2005 9:38 am
by timvw
So you need a regular expression (we have a dedicated section for that)
My first try: #<a\s+href\s*=\s*\"(.*)"\s*>#
Posted: Sun Jul 31, 2005 9:43 am
by RobertGonzalez
Thanks timvw. And I apologize for putting this into the PHP - Code category. I just wasn't sure where to start. I will by trying out your suggestion in just a little bit. Thanks again.
Posted: Sun Jul 31, 2005 10:07 am
by timvw
Code: Select all
$contents = preg_replace("#<a\s+href\s*=\s*\"(((?<!http:\/\/)[^\"])+)\"\s*>#", "<a href=\"$1&fooooo\">", $contents);
Posted: Sun Jul 31, 2005 11:18 am
by RobertGonzalez
OK, I know that this is going to sound really amateurish, but I am getting an error (I think because of the syntax I am using).
I used your code (which works perfectly on my static test data by the way) and tried to run it through my app. This is what I did:
Code: Select all
$news_data['item_content'] = preg_replace("#<a\s+href\s*=\s*\"(((?<!http:\/\/)[^\"])+)\"\s*>#", "<a href=\"" . append_sid($1) . "\">", $news_data['item_content']);
Doing it this way I am getting the following error:
Parse error: parse error, expecting `T_VARIABLE' or `'$'' in
FILENAME on line
48
Do you know of a way to take the "$1" value and run it through a function and return the functions returned value as $1?
I am really very sorry about my lack of knowledge in this area. I am just now getting into Reg Exp's.
Posted: Sun Jul 31, 2005 11:33 am
by timvw
Posted: Sun Jul 31, 2005 4:25 pm
by RobertGonzalez
Thanks again timvw. I wonder, were you a proofreader or editor for the PHP Manual?
I still could not get what I wanted out of the function that I had, but thanks to your previous posts I was able to put together a decent, fast hack that gives me just what I want. So many thanks and applause once again.
Posted: Sun Jul 31, 2005 6:27 pm
by timvw
Everah wrote:Thanks again timvw. I wonder, were you a proofreader or editor for the PHP Manual?
I think i've spend too much time in frustrating details: Why there is strtolower and str_replace (why sometimes a _ after str?)
Everah wrote:I still could not get what I wanted out of the function that I had, but thanks to your previous posts I was able to put together a decent, fast hack that gives me just what I want. So many thanks and applause once again.
Assuming you wanted to run append_sid on the matched url..
Code: Select all
$news_data['item_content'] = preg_replace_callback("#<a\s+href\s*=\s*"(((?<!http:\/\/)[^"])+)"\s*>#", "append_sid", $news_data['item_content']);