scottjcampbell Posted January 21, 2009 Share Posted January 21, 2009 Hi, i have been really annoyed with this for a while now, i am tring to replace only part of my text, but keep the middle bit in tact, e.g. i want to replace <img src="image.png" /> to: [img=image.png] I am aware this is BBCode but i am just trying to explain the problem, i am not very good at that Thanks for any help, Scott. Quote Link to comment https://forums.phpfreaks.com/topic/141843-solved-replace-part-of-a-piece-of-text/ Share on other sites More sharing options...
rhodesa Posted January 21, 2009 Share Posted January 21, 2009 print preg_replace('/<img src="(.+?)" />/','[_img]$1[/_img]',$text); edit: i had to put underscores in the IMG bbcode to make it show Quote Link to comment https://forums.phpfreaks.com/topic/141843-solved-replace-part-of-a-piece-of-text/#findComment-742650 Share on other sites More sharing options...
scottjcampbell Posted January 21, 2009 Author Share Posted January 21, 2009 i cant get it working? i tried: $message = preg_replace('/<img src="(.+?)" />/','[img=$1]',$message); but no luck. - $message appears empty? Quote Link to comment https://forums.phpfreaks.com/topic/141843-solved-replace-part-of-a-piece-of-text/#findComment-742660 Share on other sites More sharing options...
scottjcampbell Posted January 21, 2009 Author Share Posted January 21, 2009 I tried to fix the problem by removing the two slashes inside the first bracket thingy, though i get an output, i get 2 < > on the outside? $message = preg_replace('<img src="(.+?)" />','[img=$1]',"<img src=\"scott\" />"); goes to this: <[_img]scott[_/img]> **EDIT ALSO ADDED _ ON IMAGES FOR SAME REASON** Its probably because i removed the slashes but that is the only way i could even get an output for message? Any Suggestions? Thanks, Scott Quote Link to comment https://forums.phpfreaks.com/topic/141843-solved-replace-part-of-a-piece-of-text/#findComment-742669 Share on other sites More sharing options...
DarkWater Posted January 21, 2009 Share Posted January 21, 2009 echo preg_replace('~<img src="(.+?)" />~is','[_img]$1[/_img]',$text); There you go; that should work. Quote Link to comment https://forums.phpfreaks.com/topic/141843-solved-replace-part-of-a-piece-of-text/#findComment-742671 Share on other sites More sharing options...
scottjcampbell Posted January 21, 2009 Author Share Posted January 21, 2009 Yayy - Thanks it works perfectly Thanks both of u, you've been a great help. Scott Campbell Quote Link to comment https://forums.phpfreaks.com/topic/141843-solved-replace-part-of-a-piece-of-text/#findComment-742674 Share on other sites More sharing options...
scottjcampbell Posted January 21, 2009 Author Share Posted January 21, 2009 Hello Again, i have one last problem, is it possible to replace the string, even if it is not exact, i will use the same example because as i said, i can't explain things. Say i have an image, <img src="scott" /> that works fine, but what if my image has parameters with it? e.g. <img src=scott" width="100px" height="100px" border="0"> ^You know what i mean, it could have any number of differently ordered ones. e.g. height could come before width? So is it possible to make this work using the same bbcode as above? Even if it doesn't specify any parameters? - Just the URL to image? I only really care about height and width, though more would be good aswell, i already have the bbcode for this [_img=100x100]image.png[/_img] So is this possible? Thanks for any help, Scott. Quote Link to comment https://forums.phpfreaks.com/topic/141843-solved-replace-part-of-a-piece-of-text/#findComment-742704 Share on other sites More sharing options...
scottjcampbell Posted January 21, 2009 Author Share Posted January 21, 2009 Dont worry Quote Link to comment https://forums.phpfreaks.com/topic/141843-solved-replace-part-of-a-piece-of-text/#findComment-742709 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.