I want to echo a string of an url variable:
echo "direct=1&closeBrowser=1&savelog=log.txt&storage=xfile¯file=klanta\test.html";
except the '¯' 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¯file=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
Related
I am learning PHP and I am trying to use
htmlentities but he won't print it as HTML
$string2 = '<h1>Hello</h1>';
echo htmlentities($string2);
he just print it as <h1>Hello</h1>
what I am doing wrong?
htmlentities aims to escape special characters to display them instead of interpreting them. If you want your browser interprets HTML tags, you just have to echo the HTML code.
$string2 = '<h1>Hello</h1>';
echo $string2;
$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);
I'm trying to check if a string is start with '€' or '£' in PHP.
Below are the codes
$text = "€123";
if($text[0] == "€"){
echo "true";
}
else{
echo "false";
}
//output false
If only check a single char, it works fine
$symbol = "€";
if($symbol == "€"){
echo "true";
}
else{
echo "false";
}
// output true
I have also tried to print the string on browser.
$text = "€123";
echo $text; //display euro symbol correctly
echo $text[0] //get a question mark
I have tried to use substr(), but the same problem occurred.
Characters, such as '€' or '£' are multi-byte characters. There is an excellent article that you can read here. According to the PHP docs, PHP strings are byte arrays. As a result, accessing or modifying a string using array brackets is not multi-byte safe, and should only be done with strings that are in a single-byte encoding such as ISO-8859-1.
Also make sure your file is encoded with UTF-8: you can use a text editor such as NotePad++ to convert it.
If I reduce the PHP to this, it works, the key being to use mb_substr:
<?php
header ('Content-type: text/html; charset=utf-8');
$text = "€123";
echo mb_substr($text,0,1,'UTF-8');
?>
Finally, it would be a good idea to add the UTF-8 meta-tag in your head tag:
<meta charset="utf-8">
I suggest this as the easiest solution to you. Convert the symbols to their unicode identifiers using htmlentities().
htmlentities($text, ENT_QUOTES, "UTF-8");
Which will either give you £ or €. Now that allows you to run a switch() {case:} statement to check. (Or your if statements)
$symbols = explode(";", $text);
switch($symbols[0]) {
case "£":
echo "It's Pounds";
break;
case "&euro":
echo "It's Euros";
break;
}
Working Example
This happens because you’re using a multi-byte character encoding (probably UTF-8) in which both € and £ are recorded using multiple bytes. That means that "€" is a string of three bytes, not just one.
When you use $text[0] you're getting only the first byte of the first character, and so it doesn't match the three bytes of "€". You need to get the first three bytes instead, to check whether one string starts with another.
Here’s the function I use to do that:
function string_starts_with($string, $prefix) {
return substr($string, 0, strlen($prefix)) == $prefix;
}
The question mark appears because the first byte of "€" isn’t enough to encode a whole character: the error is indicated by ‘�’ when available, otherwise ‘?’.
So what I am trying to do is to match a regular expression which has an opening <p>; tag and a closing </;p> tag.This is the code I wrote:
<?php
$input = "<p>just some text</p> more text!";
$input = preg_replace('/<p>[^(<\/p>)]+?<\/;p>/','<p>$1</p>',$tem);
echo $input;
?>
So the code does not seem to replace <p> with <p> or replace </p> with </p>.I think the problem is in the part where I am checking all characters expect '</p>. I don't think the code [^(<\/p>)] is grouping all the characters correctly. I think it checks if any of the characters are not present and not if the entire group of characters is not present. Please help me out here.
[] in a RegEx is a character group, you can not match strings this way, only characters or unicode codepoints.
If you have escaped HTML entities, you can use htmlspecialchars_decode() to convert them back into characters.
After you have valid HTML, you can use the DOM to to parse, traverse and manipulate it.
How do you parse and process HTML/XML in PHP?
I think i figured it out.Here is the code:
<?php
$input = "<p>text</p>";
$tem = $input;
$tem = htmlspecialchars($input);
$tem = preg_replace('/<p>(.+?)<\/p>/','<p>$1</p>',$tem);
echo $tem;
?>
You don't need to capture the content between p tags, you only need to replace p tags:
$html = preg_replace('~<(/?p)>~', '<$1>', $html);
However, you don't regex too:
$trans = array('<p>' => '<p>', '</p>' => '</p>');
$html = strtr($html, $trans);
At least part of the trouble you're having is probably due to the fact that you seem to be playing fast and loose with the semicolons in your HTML entities. They always start with an ampersand, and end with a semicolon. So it's >, not > as you have scattered through your post.
That said, why not use html_entity_decode(), which doesn't require abusing regular expressions?
$string = 'shoop <p>da</p> woop';
echo html_entity_decode($string);
// output: shoop <p>da</p> woop
I would like to know If I can use a php function on a variable that contains delimiters like ' or ". I'm receiving a text content from my database and I would like to use: strip_tags($desc); on it but it does not work.
Here is an example of what it can contain using var dump:
string(1039) ""txt txt . txt'txt txt txtxtxtxt& " "
I guess you want to remove all tags. You should use the builtin function strip_tags() instead.
I'm assuming you want to work on the variable, not strip out the tags, then use this:
<?php
$str = "A 'quote' is <b>bold</b>";
// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str);
// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str, ENT_QUOTES);
?>
htmlentities, will make your ' & " safe to handle, then you can convert them back after if needed.
Reference for code: http://us2.php.net/manual/en/function.htmlentities.php
Try not to use ereg_replace as it is going to be discontinued.
ereg_replace
This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.
strip_tags
That said do you want to change all those chars to empty or are you trying to strip the tags? You can also convert the chars to the html_entities.
$desc = strip_tags($desc);