PHP - Convert HTML Symbol to HTML Entity - php

$html_str = "Random Text™ First Second Third";
echo $str = html_entity_decode($html_str);
outputs: Random Text™ First Second Third
now how do I convert $str to $html_str?

Per the documentation:
"html_entity_decode() is the opposite of htmlentities()..."
http://php.net/manual/en/function.html-entity-decode.php
"htmlentities — Convert all applicable characters to HTML entities"
http://php.net/manual/en/function.htmlentities.php
You want to use htmlentities
$html_str = htmlentities($str);

Related

php - how to prevent echo &macr creates ¯ character,

I want to echo a string of an url variable:
echo "direct=1&closeBrowser=1&savelog=log.txt&storage=xfile&macrfile=klanta\test.html";
except the '&macr' creates the ¯ characters. How do i prevent this from happening?
htmlspecialchars function is used to convert special characters to HTML entities. See for reference, https://www.php.net/manual/en/function.htmlspecialchars.php
You can use this code :
$str = direct=1&closeBrowser=1&savelog=log.txt&storage=xfile&macrfile=klanta\test.html
echo htmlspecialchars($str);
OR you can use regular Expression :
echo preg_replace('/[^a-zA-Z0-9_ -]/s','', $str);
If you want to print a strings as raw text (ignoring any html special entities or tags), then use function htmlspecialchars($string).
Example:
echo htmlspecialchars($your_string);
Details: https://www.php.net/manual/en/function.htmlspecialchars.php

PHP replace : find and replace the same characters with different text

How can I find and replace the same characters in a string with two different characters? I.E. The first occurrence with one character, and the second one with another character, for the entire string in one go?
This is what I'm trying to do (so users need not type html in the body): I've used preg_replace here, but I'll willing to use anything else.
$str = $str = '>>Hello, this is code>> Here is some text >>This is more code>>';
$str = preg_replace('#[>>]+#','[code]',$str);
echo $str;
//output from the above
//[code]Hello, this is code[code] Here is some text [code]This is more code[code]
//expected output
//[code]Hello, this is code[/code] Here is some text [code]This is more code[/code]
But problem here is, both >> get replaced with [code]. Is it possible to somehow replace the first >> with [code] and the second >> with a [/code] for the entire output?
Does php have something to do this in one go? How can this be done?
$str = '>>Hello, this is code>> Here is some text >>This is more code>>';
echo preg_replace( "#>>([^>]+)>>#", "[code]$1[/code]", $str );
The above will fail if something like the following is your input:
>>Here is code >to break >stuff>>
To deal with this, use negative lookahead:
#>>((?!>[^>]).+?)>>#
will be your pattern.
echo preg_replace( "#>>((?!>[^>]).+?)>>#", "[code]$1[/code]", $str );

Any more efficient way for custom preg_replace function?

I plan to use my custom function below while getting from data from my mysql table & print it as html. Since htmlspecialchars() translate tags to html entities, I retranslate them ( p, br, strong) to tags. My question is: Is it efficient enough or Is there any other shorter or more efficient way to achieve this aim? If you know any, can you please guide me with at least keywords? I can look fort he details in php.net and this site. Thanks, regards
function safe_output_from_mysql($safe_echo_to_html)
{
$safe_echo_to_html = mb_convert_encoding($safe_echo_to_html, 'UTF-8', mb_detect_encoding($safe_echo_to_html));
$safe_safe_echo_to_html = htmlspecialchars($safe_echo_to_html, ENT_QUOTES, "UTF-8");
$safe_echo_to_html = preg_replace("<br />","<br />",$safe_echo_to_html);
$safe_echo_to_html = preg_replace("<p>","<p>",$safe_echo_to_html);
$safe_echo_to_html = preg_replace("</p>","</p>",$safe_echo_to_html);
$safe_echo_to_html = preg_replace("<strong>","<strong>",$safe_echo_to_html);
$safe_echo_to_html = preg_replace("</strong>","</strong>",$safe_echo_to_html);
return $safe_echo_to_html;
}
There is no need to call preg_replace() multiple times. You can use a single pattern to match all the desired tags:
preg_replace('/<\s*(\/?(?:strong|p|br)\s*\/?)>/i', '<\1>', $s);
I'm assuming, of course, that you're actually planning to use regex to do the match. If the search strings are straight text, then strtr() is more efficient.
htmlspecialchars_decode: http://www.php.net/manual/en/function.htmlspecialchars-decode.php
This function is the opposite of htmlspecialchars(). It converts special HTML entities back to characters.
$str = "<p>this -> "</p>\n";
echo htmlspecialchars_decode($str);
The above example will output:
<p>this -> "</p>
Please see the function htmlspecialchars_decode($str); function.

PHP - convert a string with - or + signs to HTML

How do I convert a string that has a - or + sign to a html friendly string?
I mean to convert those characters to html notations, like space is and so on...
ps: htmlentities doesn't work. I still see the -/+
Try this
$string = str_replace('+', '+', $string); // Convert + sign
$string = str_replace('-', '-', $string); // Convert - sign
I don't think there is entities for these symbols see: http://www.w3schools.com/tags/ref_entities.asp
I tested with
$str = "- and +"; echo htmlentities($str);
and didn't get entities. According to: http://us.php.net/manual/en/function.htmlentities.php
I would expect them to be encoded if there was encoding available.
No idea what you want to accomplish. But this escapes selected characters to html entities:
$html = preg_replace("/([+-])/e", '"&#".ord("$1").";"', $html);
As far as I am aware, - and + are fine in HTML, and dont have an entity equivalent. See http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
Are you sure you're not thinking of URL encoding?
Specify that you want it to use unicode as follows:
htmlentities($str, ENT_QUOTES | ENT_IGNORE, "UTF-8");
Have a look at the 2nd comment on this page:
http://www.php.net/manual/en/function.htmlentities.php#100388
This will enable more encoding characters.
If you just want to encode some, then this is a little lighter weight:
<?php
$ent = array(
'+'=>'+',
'-'=>'+'
);
echo strtr('+ and -', $ent);
?>

preg_replace only OUTSIDE tags ? (... we're not talking full 'html parsing', just a bit of markdown)

What is the easiest way of applying highlighting of some text excluding text within OCCASIONAL tags "<...>"?
CLARIFICATION: I want the existing tags PRESERVED!
$t =
preg_replace(
"/(markdown)/",
"<strong>$1</strong>",
"This is essentially plain text apart from a few html tags generated with some
simplified markdown rules: <a href=markdown.html>[see here]</a>");
Which should display as:
"This is essentially plain text apart from a few html tags generated with some simplified markdown rules: see here"
... BUT NOT MESS UP the text inside the anchor tag (i.e. <a href=markdown.html> ).
I've heard the arguments of not parsing html with regular expressions, but here we're talking essentially about plain text except for minimal parsing of some markdown code.
Actually, this seems to work ok:
<?php
$item="markdown";
$t="This is essentially plain text apart from a few html tags generated
with some simplified markdown rules: <a href=markdown.html>[see here]</a>";
//_____1. apply emphasis_____
$t = preg_replace("|($item)|","<strong>$1</strong>",$t);
// "This is essentially plain text apart from a few html tags generated
// with some simplified <strong>markdown</strong> rules: <a href=
// <strong>markdown</strong>.html>[see here]</a>"
//_____2. remove emphasis if WITHIN opening and closing tag____
$t = preg_replace("|(<[^>]+?)(<strong>($item)</strong>)([^<]+?>)|","$1$3$4",$t);
// this preserves the text before ($1), after ($4)
// and inside <strong>..</strong> ($2), but without the tags ($3)
// "This is essentially plain text apart from a few html tags generated
// with some simplified <strong>markdown</strong> rules: <a href=markdown.html>
// [see here]</a>"
?>
A string like $item="odd|string" would cause some problems, but I won't be using that kind of string anyway... (probably needs htmlentities(...) or the like...)
You could split the string into tag‍/‍no-tag parts using preg_split:
$parts = preg_split('/(<(?:[^"\'>]|"[^"<]*"|\'[^\'<]*\')*>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
Then you can iterate the parts while skipping every even part (i.e. the tag parts) and apply your replacement on it:
for ($i=0, $n=count($parts); $i<$n; $i+=2) {
$parts[$i] = preg_replace("/(markdown)/", "<strong>$1</strong>", $parts[$i]);
}
At the end put everything back together with implode:
$str = implode('', $parts);
But note that this is really not the best solution. You should better use a proper HTML parser like PHP’s DOM library. See for example these related questions:
Highlight keywords in a paragraph
Regex / DOMDocument - match and replace text not in a link
First replace any string after a tag, but force your string is after a tag:
$t=preg_replace("|(>[^<]*)(markdown)|i",'$1<strong>$2</strong>',"<null>$t");
Then delete your forced tag:
$show=preg_replace("|<null>|",'',$show);
You could split your string into an array at every '<' or '>' using preg_split(), then loop through that array and replace only in entries not beginning with an '>'. Afterwards you combine your array to an string using implode().
This regex should strip all HTML opening and closing tags: /(<[.*?]>)+/
You can use it with preg_replace like this:
$test = "Hello <strong>World!</strong>";
$regex = "/(<.*?>)+/";
$result = preg_replace($regex,"",$test);
actually this is not very efficient, but it worked for me
$your_string = '...';
$search = 'markdown';
$left = '<strong>';
$right = '</strong>';
$left_Q = preg_quote($left, '#');
$right_Q = preg_quote($right, '#');
$search_Q = preg_quote($search, '#');
while(preg_match('#(>|^)[^<]*(?<!'.$left_Q.')'.$search_Q.'(?!'.$right_Q.')[^>]*(<|$)#isU', $your_string))
$your_string = preg_replace('#(^[^<]*|>[^<]*)(?<!'.$left_Q.')('.$search_Q.')(?!'.$right_Q.')([^>]*<|[^>]*$)#isU', '${1}'.$left.'${2}'.$right.'${3}', $your_string);
echo $your_string;

Categories