[SOLVED] - String replacement inside another string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

[SOLVED] - String replacement inside another string

Post 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.
Last edited by RobertGonzalez on Sun Jul 31, 2005 4:26 pm, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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=&quote;&#1111;SOMEHYPERLINK]&quote;>
and replace the [SOMEHYPERLINK] value as long as that value does NOT contain the "http://" string?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

So you need a regular expression (we have a dedicated section for that)

My first try: #<a\s+href\s*=\s*\"(.*)"\s*>#
Last edited by timvw on Sun Jul 31, 2005 9:47 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

$contents = preg_replace("#<a\s+href\s*=\s*\"(((?<!http:\/\/)[^\"])+)\"\s*>#", "<a href=\"$1&fooooo\">", $contents);
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Thanks again timvw. I wonder, were you a proofreader or editor for the PHP Manual? :D

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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Everah wrote:Thanks again timvw. I wonder, were you a proofreader or editor for the PHP Manual? :D
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']);
Post Reply