简体   繁体   中英

Remove slash str_replace PHP

im trying to remove the unwanted characters from a string, but i cannot figure out with the slash / one.

$url = "http://www.google.com/";
$array_remove = array(
  '/',
  ' ',
  '-',
  '.'  
);

$string = "This i Will convert to-Picture/sting";

$convert = $url.'images/'.strtolower(str_replace($array_remove, '_',$string).'.gif');

in this case the slash will remain there and the result will be: this_i_will_convert_to_picture/string.gif

But need to be this_i_will_convert_to_picture_string.gif

Any help or hint here is very well appreciated.

Your code works perfectly in Version 5.4.0.
Try preg_replace:

  $string = preg_replace('(\/)', '_', $string);  
  echo "<br />string:".$string."<br />";  

Are you maybe fetching the data from a POST?

The conversion dont want to work in this scenario, so i changed the scenario to save the final string and use there where i want.

Is the same thing, but in this scenario work.

$url = "http://www.google.com/";
$array_remove = array(
  '/',
  ' ',
  '-',
  '.'  
);
$string = "This i Will convert to-Picture/sting";
$converted = strtolower(str_replace($array_remove, '_', $string));

$convert = $url.'images/'.$converted.'.gif';

Excellent! Your code works perfectly.
Run this code:

 $url = "http://www.google.com/";
 $array_remove = array( '/', ' ',  '-',  '.'  );
 $string = "This i Will convert to-Picture/sting";
 echo "string: ".$string."<br />";
 $converted = strtolower(str_replace($array_remove, '_', $string));
 echo "converted: ".$converted."<br />";
 $convert = $url.'images/'.$converted.'.gif';
 echo "convert: ".$convert."<br />";

and your output should be:

string: This i Will convert to-Picture/sting
converted: this_i_will_convert_to_picture_sting
convert: http://www.google.com/images/this_i_will_convert_to_picture_sting.gif

or what output are you getting?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM