I'm trying to convert
to whitespace
.
and then use preg_replace
to do some Regex.
like this.
$title = " TEST Ok.2-2";
$title = mb_convert_encoding($title, 'UTF-8', 'HTML-ENTITIES');
//$title = html_entity_decode($title, ENT_NOQUOTES, 'UTF-8');
//( MEAN: I can use mb_convert_encoding() or html_entity_decode())
//GOT the same out put = TEST < Ok.2-2.
//So now I have TEST < Ok.2-2
//I want to make a space on Ok so I use preg_replace()
$replace = "~\s+(ok[.]?)~i";
$title = preg_replace($replace, ' OK. ', $title, -1);
$title = preg_replace('/\s+/', ' ', $title);
$title = trim($title);
//The result = TEST < Ok.2-2 (not work!)
echo($title);
with this code the mb_convert_encoding
and html_entity_decode
is work well but when I try to use preg_replace
to regex the whitespace it seem it not found the whitespace that converted.
Now out put: TEST < Ok.2-2
Expected out put: TEST < OK. 2-2
NOW MY SOLUTION
I added the str_replace
to hardcode replace a
to whitespace
and use mb_convert_encoding or html_entity_decode to convert another htmlentity.
$title = ' TEST < Ok.2-2';
$title = str_replace(' ', ' ', $title);
$title = mb_convert_encoding($title, 'UTF-8', 'HTML-ENTITIES');
//$title = html_entity_decode($title, ENT_NOQUOTES, 'UTF-8');
//( MEAN: I can use mb_convert_encoding() or html_entity_decode())
//GOT the same out put = TEST < Ok.2-2.
//So now I have TEST < Ok.2-2
//I want to make a space on Ok so I use preg_replace()
$replace = '~\s+(ok[.]?)~i';
$title = preg_replace($replace, ' OK. ', $title, -1);
$title = preg_replace('/\s+/', ' ', $title);
$title = trim($title);
//The result TEST < OK. 2-2 (WORK!)
echo($title);
NOW my out put: TEST < OK. 2-2
MY expected: TEST < OK. 2-2
Any suggestion for best solution?
I think this will give you what you are after.
This will:
trim
)preg_replace('~\s+~', ' '
)
to a single space (str_ireplace
)ok.
case insensitive toOK.
(str_ireplace
)Output:
Your HTML entity decode example is correct, http://sandbox.onlinephpfunctions.com/code/eed7e30d507f7197585f29c1fdde9e7744fc572d
Output:
Edit:
It's probably safer to just remove the 2 entities with the str_replace. If your string were
<h1> TEST < Ok.2-2</h1>
and you decoded then removed all<
your string would not function as it had.Output: