2
$fetched_column['title'] = str_replace(' ', '<SP>', $fetched_column['title']);

echo $fetched_column['title'] . '<br>';

When it echos out it strips the white_space but doesn't replace it with <SP>. I'm guessing because of the < >. Don't know how to fix this so it echos out <SP> in replace of the white_space?

PirateTube
  • 97
  • 1
  • 10
  • 2
    Because `` is seen as an HTML tag by the browser and not displayed (view the source). You want `<` and `>`. – AbraCadaver Apr 18 '17 at 18:08
  • 1
    You need to use `htmlspecialchars()` function to make it display into `<` and `>`. Technically it shouldn't be ``, but instead `<SP>`. – Praveen Kumar Purushothaman Apr 18 '17 at 18:09
  • You shouldn't use str_replace it is deprecated function. You should use preg_replace instead – Peter Apr 18 '17 at 18:12
  • 1
    @Peter Could you highlight where exactly http://php.net/manual/en/function.str-replace.php is deprecated? – Praveen Kumar Purushothaman Apr 18 '17 at 18:13
  • 1
    @Peter must be confused with `ereg_replace`. – AbraCadaver Apr 18 '17 at 18:14
  • Hmmm, I've got that warning for years, but it was years ago, then i stopped to use it and recognize that fact... but I just checked and there is no clue about that.. I'm pretty sure about that, I remember ereg_replace too, but finally i was wrong, thank you for clarify guys! – Peter Apr 18 '17 at 18:16

2 Answers2

0

If you see view-source you can see your replaced code but because HTML tries to parse it as something (eg. directive), you dont see it in parsed web view.

You can use html entities for exactly that reason.

code would be:

$fetched_column['title'] = str_replace(' ', '&lt;SP&gt;', $fetched_column['title']);

echo $fetched_column['title'] . '<br>';
Simos Fasouliotis
  • 1,383
  • 2
  • 16
  • 35
  • Yeah that worked like a charm, don't have enough points to up vote you though hope someone does in the near future who stumbles upon this page with the same question. – PirateTube Apr 18 '17 at 18:13
0

You need to use htmlspecialchars() or htmlentities() function to make it display into &lt; and &gt;. Technically it shouldn't be <SP>, but instead &lt;SP&gt;.

Change your code to:

echo htmlentities($fetched_column['title']) . '<br>';

Or you can do it in the first attempt, when you are trying to replace, as the correct format:

$fetched_column['title'] = str_replace(' ', '&lt;SP&gt;', $fetched_column['title']);
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252