ballhogjoni Posted September 25, 2008 Share Posted September 25, 2008 Hi all, I am trying to find a url in a string and replace with another url but I can't seem to get this to work. I know the first parameter is correct. Any help would be great! $sCardHoldersSay = preg_replace( '/(http:\/\/www1\.xxxxxxxxx\.com\/partners\/links\/cardholders\/details\.asp\?idmin=)([0-9]+)(&tempid=568261)/',FACS_href.'/card-holders-say/'.$url_1.'/'.preg_replace('/ /','-',$sIssuer).'.html',$sDefinitionList ); Quote Link to comment https://forums.phpfreaks.com/topic/125868-solved-trying-to-preg_replace-but-not-working/ Share on other sites More sharing options...
nrg_alpha Posted September 26, 2008 Share Posted September 26, 2008 While I cannot say for sure.. here are a few things that come to mind.. I would simplify the pattern: $sCardHoldersSay = preg_replace('#http://www1\.xxxxxxxxx\.com/partners/links/cardholders/details\.asp\?idmin=\d+&tempid=568261#', FACS_href.'/card-holders-say/'.$url_1.'/'.preg_replace('#\x20#','-',$sIssuer).'.html',$sDefinitionList); What I have done here: - is replace the / delimiters with #. This way, you don't have to escape forward slashes. - I don't think parenthesis are needed. - simplified stuff like [0-9] to \d. - I personally can't stand seeing a space in a pattern, so I replaced your space in the nested preg_replace with \x20 (this is just my personal preference). It is hard to know what is wrong if we don't know: - what errors you are seeing PHP generate - what the value of $sIssuer is. - what the value of $sDefinitionList is. Is it possible to display an actual string example (or a fake sample that accurately reflects the real thing)? I just think there is too little info to go by (unless it is all there and I am simply missing something). Cheers, NRG Quote Link to comment https://forums.phpfreaks.com/topic/125868-solved-trying-to-preg_replace-but-not-working/#findComment-650871 Share on other sites More sharing options...
ballhogjoni Posted September 26, 2008 Author Share Posted September 26, 2008 ty for the help in simplifying the code. $sIssuer is just a string like: 'a b c d' $sDefinitionList is a string about the size of a paragraph. It contains urls, and other things, but none the less, its a string. Quote Link to comment https://forums.phpfreaks.com/topic/125868-solved-trying-to-preg_replace-but-not-working/#findComment-650874 Share on other sites More sharing options...
nrg_alpha Posted September 26, 2008 Share Posted September 26, 2008 Are you getting specific PHP errors? Or is it just not giving you the results you are looking for? Quote Link to comment https://forums.phpfreaks.com/topic/125868-solved-trying-to-preg_replace-but-not-working/#findComment-650878 Share on other sites More sharing options...
ballhogjoni Posted September 26, 2008 Author Share Posted September 26, 2008 No errrors, i figured it out. I decided to echo out the result and see what the isszue was, come to find out I forgot I had another preg_replace() rewriting what I change earlier. So I was rewriting the url on accident. Its fixed now though. Sorry for wasting your time. Quote Link to comment https://forums.phpfreaks.com/topic/125868-solved-trying-to-preg_replace-but-not-working/#findComment-650888 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.