简体   繁体   English

删除斜线str_replace PHP

[英]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 在这种情况下,斜杠将保留在那里,结果将是: this_i_will_convert_to_picture/string.gif

But need to be this_i_will_convert_to_picture_string.gif 但需要是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. 您的代码在5.4.0版中可以完美运行。
Try preg_replace: 试试preg_replace:

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

Are you maybe fetching the data from a POST? 您是否可能从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 字符串:这我将转换为图片/ sting
converted: this_i_will_convert_to_picture_sting 已转换:this_i_will_convert_to_picture_sting
convert: http://www.google.com/images/this_i_will_convert_to_picture_sting.gif 转换: http : //www.google.com/images/this_i_will_convert_to_picture_sting.gif

or what output are you getting? 或您得到什么输出?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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