-1

this is my code:

$content = str_replace("×", "x", $content);

The problem that i can replace any word but I can't replace something like × how can solve this issue ?

Be known that × is not x letter it's a symbol.

1 Answers1

0

What you are trying to do is called transliteration, heres a similar thread: How to convert special characters to normal characters?

this may work:

$content = iconv('utf-8', 'ascii//TRANSLIT', $content);

I'm not sure why your approach isn't working for you because I tested it on phpfiddle.org, and this works:

<?php
$content = "some string with × symbol";
$content = str_replace("×", "x", $content);
echo $content;
?>

I also tested preg_replace on: http://www.phpliveregex.com and this works:

preg_replace("/×/", "x", $input_lines);

Its always best to wrap strings in single quotes when they don't have special characters intended to be interpreted by PHP. If this issue is due to PHP interpreting the × symbol for some reason, this will fix it:

$content = str_replace('×', "x", $content);
Community
  • 1
  • 1
John Slotsky
  • 171
  • 1
  • 2
  • 12
  • Special characters within single quotes won't be parsed at all (backslash or not). They will be kept as written. I don't really see the point of using `preg_replace()` instead of `str_replace()` in this case. What would regular expressions bring to the table (except overhead) when just replacing a literal string? – M. Eriksson Mar 19 '17 at 13:01
  • Thank you, I solve my problem by check the page source then I found that it appears as × so I add $content = str_replace("×", "x", $content); and it's working find now. –  Mar 19 '17 at 13:15
  • Magnus: Point taken about preg_replace, playing around with the flexibility can help narrow things down though. About the quotes, thats what I mean, when the string is not meant to be parsed, single quotes should be used. I meant backslashes themselves are the only thing that gets parsed, i.e. $str = '\'; will cause a parse error since the backslash will escape the closing quote. – John Slotsky Mar 19 '17 at 13:28