[SOLVED] - String replacement inside another string
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
[SOLVED] - String replacement inside another string
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.
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.
Last edited by RobertGonzalez on Sun Jul 31, 2005 4:26 pm, edited 1 time in total.
You could use http://www.php.net/output_add_rewrite_var.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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:
and replace the [SOMEHYPERLINK] value as long as that value does NOT contain the "http://" string?
How would I go about taking a string, looking for all:
Code: Select all
<a href="e;їSOMEHYPERLINK]"e;>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Code: Select all
$contents = preg_replace("#<a\s+href\s*=\s*\"(((?<!http:\/\/)[^\"])+)\"\s*>#", "<a href=\"$1&fooooo\">", $contents);- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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:
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.
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']);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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
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.
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:Thanks again timvw. I wonder, were you a proofreader or editor for the PHP Manual?![]()
Assuming you wanted to run append_sid on the matched url..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.
Code: Select all
$news_data['item_content'] = preg_replace_callback("#<a\s+href\s*=\s*"(((?<!http:\/\/)[^"])+)"\s*>#", "append_sid", $news_data['item_content']);