dr0p Posted October 8, 2008 Share Posted October 8, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/127500-preg_replace-help/ Share on other sites More sharing options...
thebadbad Posted October 8, 2008 Share Posted October 8, 2008 <?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 ?). Quote Link to comment https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-659693 Share on other sites More sharing options...
redarrow Posted October 8, 2008 Share Posted October 8, 2008 dosent work m8 Quote Link to comment https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-659698 Share on other sites More sharing options...
thebadbad Posted October 8, 2008 Share Posted October 8, 2008 What isn't working? Output: bla bla <a href="http://mysite.com/second_url.php">link text</a> bla bla Quote Link to comment https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-659712 Share on other sites More sharing options...
dr0p Posted October 8, 2008 Author Share Posted October 8, 2008 Okay, I tried it, and it didn't work. Btw: Why is there a '~is' at the end of it? Quote Link to comment https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-660357 Share on other sites More sharing options...
xtopolis Posted October 9, 2008 Share Posted October 9, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-660508 Share on other sites More sharing options...
nrg_alpha Posted October 9, 2008 Share Posted October 9, 2008 $str = 'href="http://mysite.com/url.php?sid=randomcharacters"'; $urlParse = parse_url($str); echo $urlParse['host'] . $urlParse['path'] . '"'; Output: href="http//mysite.com/url.php" Quote Link to comment https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-660515 Share on other sites More sharing options...
nrg_alpha Posted October 9, 2008 Share Posted October 9, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-660517 Share on other sites More sharing options...
nrg_alpha Posted October 9, 2008 Share Posted October 9, 2008 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" Quote Link to comment https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-660522 Share on other sites More sharing options...
thebadbad Posted October 9, 2008 Share Posted October 9, 2008 As far as I can see, my solution is doing what you asked for. If not, at least tell us what's 'not working'. I'm no psychic, you see. Quote Link to comment https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-660773 Share on other sites More sharing options...
thebadbad Posted October 9, 2008 Share Posted October 9, 2008 Maybe it's because I assumed you were looking for hrefs inside anchor tags (<a href...). But weren't you? Quote Link to comment https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-660774 Share on other sites More sharing options...
dr0p Posted October 11, 2008 Author Share Posted October 11, 2008 Maybe it's because I assumed you were looking for hrefs inside anchor tags (<a href...). But weren't you? No I wasn't... lol. Quote Link to comment https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-663031 Share on other sites More sharing options...
thebadbad Posted October 12, 2008 Share Posted October 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/127500-preg_replace-help/#findComment-663274 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.