lily Posted December 5, 2007 Share Posted December 5, 2007 Hi, I need to find the images from some text. The images are coded within tags (I purposely put a space between img and ], so that you can see the tags). I have found a way to take off tag if there is no other text in string. For example: $RawBody="" $RawBody=preg_replace("#\[img\]([^?\['\"]+?)\.(gif|png|jpg|jpeg)\[/img\]#i", '\1.\2 ',$RawBody); I will get image name: myimage.jpg. But if there is other text in $RawBody, then I can not get right result. For example, if I change $RawBody to: $RawBody="This is my image , other text"; $RawBody=preg_replace("#\[img\]([^?\['\"]+?)\.(gif|png|jpg|jpeg)\[/img\]#i", '\1.\2 ',$RawBody); I will not be able to get result "myimage.jpg". What to do to get right image name in second case? Thanks for help! Quote Link to comment Share on other sites More sharing options...
fanfavorite Posted December 5, 2007 Share Posted December 5, 2007 Couldn't you just use str_replace? You are just trying to remove the two tags and leave everything between? $RawBody = str_replace("[img ]","",$RawBody); $RawBody = str_replace("[/img ]","",$RawBody); Quote Link to comment Share on other sites More sharing options...
lily Posted December 5, 2007 Author Share Posted December 5, 2007 Thanks for reply, fanfavorite, But I need to remove all text befor too. if $RawBody="text, , other text"; I only need result "image.jpg", nothing else. Do you know what to do? Thanks again. Quote Link to comment Share on other sites More sharing options...
fanfavorite Posted December 5, 2007 Share Posted December 5, 2007 Try this: $RawBody="text, , other text"; $ini = strpos($string,"[img ]"); $ini += strlen("[img ]"); $len = strpos($string,"[/img ]",$ini) - $ini; $newstring = substr($string,$ini,$len); echo $newstring; Quote Link to comment Share on other sites More sharing options...
lily Posted December 5, 2007 Author Share Posted December 5, 2007 It works!! Many thanks, fanfavorite. Quote Link to comment 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.