2

I have a string 90001 90002. I need to see which html special char is represented in the empty space. (It could be an empty space -   or it could be a textual line-break masquerading as a space in HTML see here. Maybe other possibilities that I'm not aware of..)

I would like to echo a string and show the empty spaces as html char (e.g. $result = "90001 90002").

I've tried using html_entities but that doesn't cover spaces. Also neither does htmlspecialchars. How would I go about doing this?
If possible I would like a purely PHP & html solution. If necessary - CSS. And if impossible otherwise, javascript will have to do..

Ben
  • 515
  • 5
  • 18

4 Answers4

3

Just an out-of-box thinking. I had the same issue but looked into using JavaScript's encodeURIComponent() function:

$(function () {
  $("div").html(function () {
    return encodeURIComponent($(this).html());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>90001 90002</div>

Used jQuery as I am kinda lazy. You can do the same in JavaScript this way:

<div>90001 90002</div>
<script>
  var div = document.querySelector("div");
  div.innerHTML = encodeURIComponent(div.innerHTML);
</script>

Just letting you know that this is better to handle in client side than in the server side.


Update 2: Getting the HTML content and updating the text of the HTML:

$(function () {
  $("div").text(function () {
    return $(this).html().replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
      return '&#'+i.charCodeAt(0)+';';
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>90001—90002</div>

Something like this...

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
-1
<?php

$mystring = "90001 90002";

$mystring = str_replace(" ","&nbsp;",$mystring);

echo $mystring;

?>

I hope it helps

TarangP
  • 2,711
  • 5
  • 20
  • 41
  • thanks! but i think you misunderstood the question. I'm not sure what the string will be. I am receiving it as an empty space and I need to print it to show what is there. (Your solution simply replaces the empty string with a character and then printing that chosen character..) – Ben Dec 26 '17 at 09:03
  • @Ben there is only java script that can you us for this – TarangP Dec 26 '17 at 09:06
-2
function removeWhiteSpace($text)
{
  $text = preg_replace('/[\t\n\r\0\x0B]/', '', $text);
  $text = preg_replace('/([\s])\1+/', ' ', $text);
  $text = trim($text);
  return $text;
}
Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
bmodanwal
  • 7
  • 1
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – yivi Dec 26 '17 at 09:24
-3

Editing the Answer :

if you need to do it using PHP , Use urlencode function , which would hep to resolve it

<?php
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>

But they have set backs which is mentioned in the notes section of the documentation. http://php.net/manual/en/function.urlencode.php

You can also use functions like htmlentities,rawurlencode in php .

if you want to use it in javascript , You can use escape function. https://www.w3schools.com/jsref/jsref_escape.asp

redhatvicky
  • 1,912
  • 9
  • 8