PHP trim unexpected behaviour - php

I am using the following function in PHP to trim some unwanted characters.
$inputString = "आनन्द मठ";
trim(html_entity_decode($inputString), " \t\n\r\0\x0B\xC2\xA0");
The above code is working fine for all cases but in one input string (आनन्द मठ) it is converting it to आनन्द म�. It has a unwanted �. Also happening for परेटो- श्रेष्ठ converted to परेटो- श्रेष्�.

trim()
This function use iso-8859 encoding.
you must use UTF8 (Unicode) function. Try this function
function mb_trim($string, $charlist='\\\\s', $ltrim=true, $rtrim=true)
{
$both_ends = $ltrim && $rtrim;
$char_class_inner = preg_replace(
array( '/[\^\-\]\\\]/S', '/\\\{4}/S' ),
array( '\\\\\\0', '\\' ),
$charlist
);
$work_horse = '[' . $char_class_inner . ']+';
$ltrim && $left_pattern = '^' . $work_horse;
$rtrim && $right_pattern = $work_horse . '$';
if($both_ends)
{
$pattern_middle = $left_pattern . '|' . $right_pattern;
}
elseif($ltrim)
{
$pattern_middle = $left_pattern;
}
else
{
$pattern_middle = $right_pattern;
}
return preg_replace("/$pattern_middle/usSD", '', $string) );
}

Add http header in your php like
header("Content-Type: text/html; charset=ISO-8859-1");
or put the encoding in a meta tag:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

Related

Avoiding equal and ampersand conversion in PHP

I have a php variable containing some special characters, inside a Codeigniter 3 controller:
page_url = 'search=' . $expression . '&page';
In a template, I use this variable:
In the in the browser I see the characters mentioned above in this form:
posts/search?search%3Dharum%26page=2
The = sign turns to %3D, "&" to %26.
I tried page_url = urldecode($page_url); but it does not work.
How do I keep the original characters?
Please use utf8 decode and try again
echo utf8_decode(urldecode("search%3Dharum%26page=2"));
Try this decode function.
function decode($url)
{
$special = array(
'%21' => '!', '\\' => '%5C', // so on you need to define.
);
foreach($special as $key => $value)
{
$result = str_replace($key, $value, $url);
}
return $result;
}
echo decode("search=%21");
Your problem is not that easy to reproduce. The following rextester demo produces the text in the form you request, using basically your code: http://rextester.com/YTY13099
<?php
$page=2;
$expression='harum';
$page_url = 'posts/search?search=' . $expression . '&page=' . $page;
?>
resulting in
Link
Could it be that the problem is caused by that fact that the script is part of a codeigniter controller? I do not know anything about codeigniter but I can image that further processing takes place there.

UTF Regex with preg_match in PHP

I need a regeular expression for german words with ä,ü etc.
When I test this regex on this website https://regex101.com/
/^\p{L}+$/u
all is fine, but on my server I upload a CSV and want to parse the words.
When I call with the word "Benedikt"
preg_match("/^[\p{L}]+$/u", $attributes[0])
I get false. The encoding of the CSV is UTF-8, when I convert it to ANSI, all is good but the ä,ü etc. is not shown correctly, so I think I should convert it to UTF-8.
But why is it returning false?
The problem occurs because your csv file starts with a UTF-8 BOM. If you remove this, the regex works perfectly. I have confirmed it with this code:
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<?php
function remove_utf8_bom($text)
{
$bom = pack('H*','EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}
$csvContents = remove_utf8_bom(file_get_contents('udfser_new.csv'));
$lines = str_getcsv($csvContents, "\n"); //parse the rows
foreach ($lines as &$row) {
$row = str_getcsv($row, ";");
$firstName = $row[0];
$lastName = $row[1];
echo 'First name: ' . $firstName . ' - Matches regex: ' . (preg_match("/^[\p{L}]+$/u", $firstName) ? 'yes' : 'no') . '<br>';
echo 'Last name: ' . $lastName . ' - Matches regex: ' . (preg_match("/^[\p{L}]+$/u", $lastName) ? 'yes' : 'no') . '<br>';
}
?>
</body>
</html>
The regex match the text successfully, and the ü in Glückmann is shown correctly on the page.
preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
http://php.net/manual/en/function.preg-match.php

PHP URL parameters append return special character

I'm programming a function to build an URL, here it is :
public static function requestContent($p_lParameters)
{
$sParameters = "?key=TEST&format=json&jsoncallback=none";
foreach($p_lParameters as $sParameterName => $sParameterValue)
{
$sParameters .= "&$sParameterName=$sParameterValue";
}
echo "<span style='font-size: 16px;'>URL : http://api.oodle.com/api/v2/listings" . $sParameters . "</span><br />";
$aXMLData = file_get_contents("http://api.oodle.com/api/v2/listings" . $sParameters);
return json_decode($aXMLData,true);
}
And I am calling this function with this array list :
print_r() result : Array ( [region] => canada [category] => housing/sale/home )
But this is very strange I get an unexpected character (note the special character none**®**ion) :
http://api.oodle.com/api/v2/listings?key=TEST&format=json&jsoncallback=none®ion=canada&category=housing/sale/home
For information I use this header :
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<?php header('Content-Type: text/html;charset=UTF-8'); ?>
EDIT :
$sRequest = "http://api.oodle.com/api/v2/listings?key=TEST&format=json&jsoncallback=none&region=canada&category=housing/sale/home";
echo "<span style='font-size: 16px;'>URL : " . $sRequest . "</span><br />";
return the exact URL with problem :
http://api.oodle.com/api/v2/listings?key=TEST&format=json&jsoncallback=none®ion=canada&category=housing/sale/home
here is the solution and this time it will work
$sParameters .= "&$sParameterName=$sParameterValue";
$sParameters = htmlentities($sParameters);
It converts all the charset into html code.. totally forgot about this even when I regualarly use it in user input...
Well, first you build a string
$sParameters = "?key=TEST&format=json&jsoncallback=none";
And then you're appending to that. So it will concatenate.
Now, the last part of a string might be & and your parameter is region.
Somehow that gets converted to the html ASCII code ® which causes the registered symbol to appear.

Unicode / UTF Seo Friendly Url (slug) Using Php Mysql

My Url Change to Seo Friendly using this function + .htaccess . My Project Is in ARABIC Language !
function clean($title) {
$seo_st = str_replace(' ', '-', $title);
$seo_alm = str_replace('--', '-', $seo_st);
$title_seo = strtolower(str_replace(' ', '', $seo_alm));
return $title_seo;}
now in my url I see This :
localhost/news/4/�����-��-����-�����-��-����/
What's Problem ?
Thanks
Try this in your code before doing anything else and tell me if it works:
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
Try this...
$dbconnect = #mysql_connect($server,$db_username,$db_password);
$charset = #mysql_set_charset('utf8',$dbconnect);
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
</head>
Check if your database field collation is properly set to UTF-8, and that your connection is UTF-8 SET NAMES "utf8".
If you're using any characters from values in your scripts, make sure they're UTF-8 as well.
Try it... it works for me
<?php
function clean_url($text)
{
$code_entities_match = array(' ','&','--','"','!','#','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','.','/','*','+','~','`','=','"');
$code_entities_replace = array('-','-','','','','','','','','','','','','','','','','','','','','','','','','');
$text = str_replace($code_entities_match, $code_entities_replace, $text);
return urlencode($text);
}
?>

native php function to highlight javascript?

Is there any native PHP function as highlight_string(); but for javascript ?
Or, if not, is there any PHP function (homemade) to do it?
EDIT: I want to use PHP function to COLORIZE javascript
I have had great success with GeSHi. Easy to use and integrate in your app and it supports a lot of languages.
I understand you want a Syntax Highligher written in PHP. This one (Geshi) has worked for me in the past:
http://qbnz.com/highlighter/
Yes, the PHP function highlight_string() is a native PHP function for PHP.
No.
But there are a lot of javascript libraries that do syntax-highlight on several languages,
from bash-scripting to php and javascript.
eg, like snippet (JQuery) or jQuery.Syntax (my favorite)
Over here you can find an excellent library which enables syntax highlighting in a large amount of languages using javascripts and a css class.
There is no native php function to do this, so either you have to use existing libraries or you have to write something yourself.
Fastest way - you can use also PHP function "highlight_string" with a little trick
(capture function output and remove leading/trailing PHP tags):
$source = '... some javascript ...';
// option 1 - pure JS code
$htmlJs = highlight_string('<?php '.$source.' ?>', true);
$htmlJs = str_replace(array('<?php ', ' ?>'), array('', ''), $htmlJs);
// option 2 - when mixing up with PHP code inside of JS script
$htmlJs = highlight_string('START<?php '.$source.' ?>END', true);
$htmlJs = str_replace(array('START<span style="color: #0000BB"><?php </span>', ' ?>END'), array('', ''), $htmlJs);
// check PHP INI setting for "highlight.keyword" (#0000BB) - http://www.php.net/manual/en/misc.configuration.php#ini.syntax-highlighting
No native function, but rather than using a full stack library just to highlight some javascript you can use this single function :
function format_javascript($data, $options = false, $c_string = "#DD0000", $c_comment = "#FF8000", $c_keyword = "#007700", $c_default = "#0000BB", $c_html = "#0000BB", $flush_on_closing_brace = false)
{
if (is_array($options)) { // check for alternative usage
extract($options, EXTR_OVERWRITE); // extract the variables from the array if so
} else {
$advanced_optimizations = $options; // otherwise carry on as normal
}
#ini_set('highlight.string', $c_string); // Set each colour for each part of the syntax
#ini_set('highlight.comment', $c_comment); // Suppression has to happen as some hosts deny access to ini_set and there is no way of detecting this
#ini_set('highlight.keyword', $c_keyword);
#ini_set('highlight.default', $c_default);
#ini_set('highlight.html', $c_html);
if ($advanced_optimizations) { // if the function has been allowed to perform potential (although unlikely) code-destroying or erroneous edits
$data = preg_replace('/([$a-zA-z09]+) = \((.+)\) \? ([^]*)([ ]+)?\:([ ]+)?([^=\;]*)/', 'if ($2) {' . "\n" . ' $1 = $3; }' . "\n" . 'else {' . "\n" . ' $1 = $5; ' . "\n" . '}', $data); // expand all BASIC ternary statements into full if/elses
}
$data = str_replace(array(') { ', ' }', ";", "\r\n"), array(") {\n", "\n}", ";\n", "\n"), $data); // Newlinefy all braces and change Windows linebreaks to Linux (much nicer!)
$data = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $data); // Regex identifies all extra empty lines produced by the str_replace above. It is quicker to do it like this than deal with a more complicated regular expression above.
$data = str_replace("<?php", "<script>", highlight_string("<?php \n" . $data . "\n?>", true));
$data = explode("\n", str_replace(array("<br />"), array("\n"), $data));
# experimental tab level highlighting
$tab = 0;
$output = '';
foreach ($data as $line) {
$lineecho = $line;
if (substr_count($line, "\t") != $tab) {
$lineecho = str_replace("\t", "", trim($lineecho));
$lineecho = str_repeat("\t", $tab) . $lineecho;
}
$tab = $tab + substr_count($line, "{") - substr_count($line, "}");
if ($flush_on_closing_brace && trim($line) == "}") {
$output .= '}';
} else {
$output .= str_replace(array("{}", "[]"), array("<span style='color:" . $c_string . "!important;'>{}</span>", "<span style='color:" . $c_string . " !important;'>[]</span>"), $lineecho . "\n"); // Main JS specific thing that is not matched in the PHP parser
}
}
$output = str_replace(array('?php', '?>'), array('script type="text/javascript">', '</script>'), $output); // Add nice and friendly <script> tags around highlighted text
return '<pre id="code_highlighted">' . $output . "</pre>";
}
Usage :
echo format_javascript('console.log("Here is some highlighted JS code using a single function !");') ;
Credit :
http://css-tricks.com/highlight-code-with-php/
Demo :
http://css-tricks.com/examples/HighlightJavaScript/
Well nice info here . Here is another nice one : http://code.google.com/p/google-code-prettify/

Categories