Jump to content

Recommended Posts

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;
}

Link to comment
https://forums.phpfreaks.com/topic/150484-solved-preg-replace-not-working/
Share on other sites

<?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.

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);
?&gt

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

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.

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);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.