gaza165 Posted July 15, 2009 Share Posted July 15, 2009 <img src="http://www.thedesignmonkeys.co.uk/img/image.png"> I need a regex pattern to find something similar to above. Quote Link to comment https://forums.phpfreaks.com/topic/166042-need-regular-expression-for-this/ Share on other sites More sharing options...
.josh Posted July 15, 2009 Share Posted July 15, 2009 preg_match('~<img src="http://www\.thedesignmonkeys\.co\.uk/img/image\.png">~',$string,$match); Quote Link to comment https://forums.phpfreaks.com/topic/166042-need-regular-expression-for-this/#findComment-875736 Share on other sites More sharing options...
nrg_alpha Posted July 15, 2009 Share Posted July 15, 2009 Something tells me there is more to it than simply looking for the above. You want to look for something similar to the above.. in what way? Here is a sample that matches that anchor exactly: Damn, CV beat me to it! So we skip the pattern and move on to... I'm not sure what you mean by something similar... is there some parts of the url that are always going to change? Is there only one specific anchor tag you seek, or is there multiple ones? When dealing with regex, you have to be crystal clear as to what you are looking for, as regex is a very terse system that needs to be precisely thought out in order for it to work. Quote Link to comment https://forums.phpfreaks.com/topic/166042-need-regular-expression-for-this/#findComment-875738 Share on other sites More sharing options...
Maq Posted July 15, 2009 Share Posted July 15, 2009 CV, j/k, I just had a strong urge to use the new smilies. Quote Link to comment https://forums.phpfreaks.com/topic/166042-need-regular-expression-for-this/#findComment-875833 Share on other sites More sharing options...
gaza165 Posted July 15, 2009 Author Share Posted July 15, 2009 Well basically what I am trying to do is... On my shoutbox, users are able to use bbcodes to add an image to the shoutbox!! like this [ img ]http://www.domain.com/image.png [/ img ] when it is displayed, i only have the thumbnail displayed, what i want to do is encase any images that posted with its url so the user can click the thumbnail to navigate to the large image. So the regex is to find this image and encase its <a> around it... What is the best way to go about this?? Am i looking at it the wrong way??? Quote Link to comment https://forums.phpfreaks.com/topic/166042-need-regular-expression-for-this/#findComment-875839 Share on other sites More sharing options...
nrg_alpha Posted July 15, 2009 Share Posted July 15, 2009 So if I understand you correctly, is this along the lines of what you are looking for? Example: $str = '[img=http://www.domain.com/image.png]'; $str = preg_replace('#([ img ]([^[]+)[ /img ])#i', '<a href="$2">$1</a>', $str); echo $str; // <a href="[url=http://www.domain.com/image.png]http://www.domain.com/image.png[/url]">[img=http://www.domain.com/image.png]</a> Note, due to the posting system trying to convert my tags in the pattern to an actual image, I inserted spaces.. but if you cut and paste this snippet to test it out, remove the spaces in the [ img ] and [ /img ] part of the pattern. Note also that in the code comment, the posting system inserts the url bbc tags as well.. ignore those. Quote Link to comment https://forums.phpfreaks.com/topic/166042-need-regular-expression-for-this/#findComment-875853 Share on other sites More sharing options...
gaza165 Posted July 15, 2009 Author Share Posted July 15, 2009 what i need to replace it with is this.... [url=http://www.domain.com/image.png][img=http://www.domain.com/image.png][/url] Quote Link to comment https://forums.phpfreaks.com/topic/166042-need-regular-expression-for-this/#findComment-875858 Share on other sites More sharing options...
.josh Posted July 15, 2009 Share Posted July 15, 2009 CV, j/k, I just had a strong urge to use the new smilies. I tend not to use code tags on one-liners. For some reason I feel it's overkill or something, idk. Quote Link to comment https://forums.phpfreaks.com/topic/166042-need-regular-expression-for-this/#findComment-875893 Share on other sites More sharing options...
nrg_alpha Posted July 15, 2009 Share Posted July 15, 2009 what i need to replace it with is this.... Right, I see (I saw you mentioned <a> tag and wasn't thinking in context of forum bbc url tags. Example: $str = '[img=http://www.domain.com/image.png]'; $$str = preg_replace('#(\[ img \]([^[]+)\[ /img \])#i', '[ url=$2 ]$1[ /url ]', $str); echo $str; // [url=http://www.domain.com/image.png][img=http://www.domain.com/image.png][/url] Once again, remove the spaces in \[ img \], \[ /img \] and [ url=$2 ]$1[ /url ] Quote Link to comment https://forums.phpfreaks.com/topic/166042-need-regular-expression-for-this/#findComment-876023 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.