AdRock Posted March 21, 2009 Share Posted March 21, 2009 I have a function that is supposed to replace some part of a string with another string but it refuses to do it and i can't see why. T here is no problem with the function replacing any other part of the string such as newline breaks and unorder lists but the problem lies with replacing the end of image tags. Say i have a string like: <img src="images/image.jpg><br> I want it to be replaced with: <img src="images/image.jpg" class="imgright" alt="" /><br /> All the <ul> and <br> are replaced properly but not the images function convert_text($content) { $reg_ex = array( '/<ul>/s', '/.jpg">/s', '/.gif">/s', '/<br>/s' ); $replace_word = array( '<ul class="list">', '.jpg" class="imgright" alt="" />', '.gif" class="imgright" alt="" />', '<br />' ); $content = preg_replace($reg_ex, $replace_word, $content); return $content; } Quote Link to comment Share on other sites More sharing options...
DJTim666 Posted March 21, 2009 Share Posted March 21, 2009 <?php function convert_text($content) { $reg_ex = array( '/<ul>/s', '/\.jpg">/s', '/\.gif">/s', '/<br>/s' ); $replace_word = array( '<ul class="list">', '.jpg" class="imgright" alt="" />', '.gif" class="imgright" alt="" />', '<br />' ); $content = preg_replace($reg_ex, $replace_word, $content); return $content; } ?> That should work. Quote Link to comment Share on other sites More sharing options...
AdRock Posted March 21, 2009 Author Share Posted March 21, 2009 It should but it doesn't I do a var_dump on it after the calling the function and the string has replaced the <br> to a <br /> but the .jpg>" tags haven't Quote Link to comment Share on other sites More sharing options...
Maq Posted March 22, 2009 Share Posted March 22, 2009 First of all, you were missing a " after the .jpg tag in your example image tag. I was working on this for like 10 minutes before I noticed it lol. Anyway I usually do array replacements like this: function convert_text($content) { $reg_ex[0] = '/</pre> <ul>/'; $reg_ex[1] = '/\.jpg">/'; $reg_ex[2] = '/\.gif">/'; $reg_ex[3] = '/ /'; $replace_word[3] = ''; $replace_word[2] = '.jpg" class="imgright" alt="" />'; $replace_word[1] = '.gif" class="imgright" alt="" />'; $replace_word[0] = ' '; $content = preg_replace($reg_ex, $replace_word, $content); return $content; } $w = ' '; echo convert_text($w); ?> Quote Link to comment Share on other sites More sharing options...
AdRock Posted March 22, 2009 Author Share Posted March 22, 2009 I tried that too but it still doesn't work What is really strange is that it works on a testing machine at home but when i upload to the server it doesn't work. I can do all that changes on my pc here including the images but not when hosted. Is there any other reason such as the server could have a different configuration or something wrong with the regex? The only other thing i can do is save to the database as is, query it and update the row but that seems really inefficient for something that should work Quote Link to comment Share on other sites More sharing options...
Maq Posted March 22, 2009 Share Posted March 22, 2009 I tried that too but it still doesn't work What is really strange is that it works on a testing machine at home but when i upload to the server it doesn't work. I can do all that changes on my pc here including the images but not when hosted. Is there any other reason such as the server could have a different configuration or something wrong with the regex? The only other thing i can do is save to the database as is, query it and update the row but that seems really inefficient for something that should work Yeah it works on the example and my localhost just fine. The only thing I can think of is checking out the permissions, although they should be fine. Quote Link to comment Share on other sites More sharing options...
AdRock Posted March 22, 2009 Author Share Posted March 22, 2009 I finally got it working. I had to use stripslashes and I don't know if it makes any difference, but i read a suggestion on somone elses probalem and they turned off magic quotes. I have commented it out so i'm not using it but it seems to work ini_set("magic_quotes_gpc", "0"); set_magic_quotes_runtime(1); 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.