I'm trying to replace the > character with # inside the HTML5 code tag . The problem is that the > code character is replaced and it gives me an error in the HTML5 source code.
This is the string in which I am looking for those characters
window.setInterval(function(){
if ($('>title_theme').val().length >= 1) {
cargarArchivo(
'POST',
'http://labex.com/PostsController',
'title_p=' + $('>title_theme').val()
);
}
The above code is wrapped in the code tag to be displayed as snippet code on my web page.
$('>title_theme') El caracter > debe ser reemplazado por #
I have tried to replace the > character but it changes the > character of the code itself , resulting in an error in the HTML5 source code (the snippet code is simply not displayed)
Use preg_replace to find and replace
$simbolos = array(
'/</uim',
'/>/uim',
'/\<code\>/uim',
'/&\>\b39/uim',
'/&\bquot/uim',
'/&\blt/uim',
'/&\bgt/uim',
'/\<\/code\>/uim',
);
$reemplazos = array(
'<',
'>',
"<div class='code'><pre class='prettyprint'><code>",
"'",
'"',
'<',
'>',
"</code></pre></div>",
);
$resultado = preg_replace($simbolos, $reemplazos, $request->getSend()->input('content'));
echo $resultado;
Without the preg_replace replacements the content would look like this:
window.setInterval(function(){
if ($(&>39>title_theme&>39).val().length &gt= 1) {
cargarArchivo(
&>39POST&>39,
&>39http://labex.com/PostsController&>39,
&>39title_p=&>39 + $(&>39>title_theme&>39).val()
);
}
Trying with the code:
$resultado=str_replace('>','#',html_entity_decode($request->getSend()->input('content')));
echo $resultado;
The result would be the following:
<code#
window.setInterval(function(){
if ($('#title_theme').val().length >= 1) {
cargarArchivo(
'POST',
'http://labex.com/PostsController',
'title_p=' + $('#title_theme').val()
);
}
</code#
It would be nice to replace all the # that are between code and /code , not to replace the < and > of the code tags
To solve it you must use
html_entity_decode()
to transform all HTML entities to their corresponding characters.Then you apply the
str_replace()
:Then to avoid changing also the
>
in the labels<code>
you can replace it one more time like this:For more information you can review the documentation
I hope it is what you are looking for, greetings.
Very well. Analyzing the code gave me an insight into the regular expressions of preg_replace. I will leave the answer
All < and > characters are replaced by # (including code and /code ), once replaced we have to reestablish the code and /code tags so that we don't get an error in HTML, so it is replaced with regular expressions.
If we do the above, the result would be like this: