Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.