Jump to content

preg_replace() Help?


dr0p

Recommended Posts

Okay, so I have looked around and I have had no luck finding any tutorial on how to write the wildcard search patterns at all.

 

What I'm searching for:

href="http://mysite.com/url.php?sid=randomcharacters" ..............

 

What I'd like to end up with:

href="http://mysite.com/second_url.php" .............

 

Any help would be much appreciated :D

Link to comment
https://forums.phpfreaks.com/topic/127500-preg_replace-help/
Share on other sites

<?php
$str = 'bla bla <a href="http://mysite.com/url.php?sid=jhgfrt6757890iuhygf">link text</a> bla bla';
$second_url = 'http://mysite.com/second_url.php';
$find = '~(<a.+?href=")http://mysite\.com/url\.php\?sid=.+?"~is';
$replace = '$1' . $second_url . '"';
$str = preg_replace($find, $replace, $str);
?>

 

Will replace hrefs with your specified URL with hrefs with the second URL. Link classes, ids, styles, etc. will be retained. If the search URL has other parameters, those will also be removed/replaced (e.g. http://mysite.com/url.php?sid=randomcharacters&name=test&ref=noone). The 'wildcard' used is .+?, meaning any character one or more times. The ? makes it un-greedy, stopping the match before the first found double quote (because we have a double quote just after the ?).

Link to comment
https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-659693
Share on other sites

He is using ~ as his delimeter since he is dealing with a url which already contains /  (the common delimeter people use). i = insensitive, s... is another modifier I don't know off hand

 

Be more specific about how it didn't work, what was the result you got, and what should it have been?

Link to comment
https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-660508
Share on other sites

If you really needed it as a regex solution:

 

$str = 'href="http://mysite.com/url.php?sid=randomcharacters"';
preg_match('#^(.+?\.php)#', $str, $match);
echo $match[1] . '"';

 

Ouput:

href="http://mysite.com/url.php"

 

EDIT: Ok.. I re-read the OP.. So if I understand correctly, you want to find a segment of a string that contains that example url and isolate the section of that url?

Link to comment
https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-660517
Share on other sites

So perhaps something like this?

 

$str = 'I am going to link to say href="http://mysite.com/url.php?sid=randomcharacters" and make lots of money through ad pay-by-clicks!';
preg_match('#(href.+?\.php)#', $str, $match);
echo $match[1] . '"';

 

Outputs:

 

href="http://mysite.com/url.php"

Link to comment
https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-660522
Share on other sites

Okay, if it's simply any href (but as a HTML attribute), try

 

<?php
$str = 'bla bla <a href="http://mysite.com/url.php?sid=jhgfrt6757890iuhygf">link text</a> bla bla';
$second_url = 'http://mysite.com/second_url.php';
$find = '~(<.+?href=")http://mysite\.com/url\.php\?sid=.+?"~is';
$replace = '$1' . $second_url . '"';
$str = preg_replace($find, $replace, $str);
?>

 

Else explain in detail why this isn't what you're looking for.

Link to comment
https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-663274
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.