I have a text such " مُشْكِلَةٌ " in db. How can I get " مشكلة " from this text in php( str_replace etc ). I have tried str_replace it didn't work
Code
$string = 'مُشْكِلَةٌ';
$diacritic = array('ِ', 'ُ', 'ٓ', 'ٰ', 'ْ', 'ٌ', 'ٍ', 'ً', 'ّ', 'َ');
$newString = str_replace($diacritic, '', $string);
echo "Old String : ".$string;
echo "New String : ".$newString;
Output
Old String : مُشْكِلَةٌ
New String : مشكلة
Demo
Related
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 1 year ago.
request
http://a.com/?q=\xC3\xA2\xB0\xED
source
$str1 = "\xC3\xA2\xB0\xED";
$str2 = '\xC3\xA2\xB0\xED';
$str3 = $_GET['q'];
echo "string1 : " . mb_detect_encoding($str1) . "<br>";
echo iconv("CP949", "UTF-8", $str1) . "<br>";
echo "string2 : " . mb_detect_encoding($str2) . "<br>";
echo iconv("CP949", "UTF-8", $str2) . "<br>";
echo "string3 : " . mb_detect_encoding($str3) . "<br>";
echo iconv("CP949", "UTF-8", $str3) . "<br>";
response
string1 : UTF-8
창고
string2 : ASCII
\xC3\xA2\xB0\xED
string3 : ASCII
\xC3\xA2\xB0\xED
$str2, $str3 decode is fail..
How can i fix it?
And Why is it different.. single quote string VS double quote string
Version : PHP 7.1.30
Thanks in advance
When you use single quote for a string each character is as it is and php doesn't interpret it, so $str2 can't convert to another encoding.
Also Query strings assumes as single quote strings so $str3 is like $str2.
And the solution is stripcslashes. it actually converts a single quote string to double quote string.
and you can fix it in this way:
$str2 = '\xC3\xA2\xB0\xED';
$str2 = stripcslashes($str2);
$str3 = $_REQUEST["q"];
$str3 = stripcslashes($str3);
I have a data base with texts and in each text there are words (tags) that start with # (example of a record : "Hi I'm posting an #issue on #Stackoverflow ")
I'm trying to find a solution to add html code to transform each tag into a link when printing the text.
So the text are stored as strings in MySQL database like this :
Some text #tag1 text #tag2 ...
I want to replace all these #abcd with
#abcd
And have a final result as follow:
Some text #tag1 text #tag2 ...
I guess that i should use some regex but it is not at all my strong side.
Try the following using preg_replace(..)
$input = "Hi I'm posting an #issue on #Stackoverflow";
echo preg_replace("/#([a-zA-Z0-9]+)/", "<a href='targetpage.php?val=$1'>#$1</a>", $input);
http://php.net/manual/en/function.preg-replace.php
A simple solution could look like this:
$re = '/\S*#(\[[^\]]+\]|\S+)/m';
$str = 'Some text #tag1 text #tag2 ...';
$subst = '#$1';
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Demo
If you are actually after Twitter hashtags and want to go crazy take a look here how it is done in Java.
There is also a JavaScript Twitter library that makes things very easy.
Try this the function
<?php
$demoString1 = "THIS is #test STRING WITH #abcd";
$demoString2 = "Hi I'm posting an #issue on #Stackoverflow";
function wrapWithAnchor($link,$string){
$pattern = "/#([a-zA-Z0-9]+)/";
$replace_with = '<a href="'.$link.'?val=$1">$1<a>';
return preg_replace( $pattern, $replace_with ,$string );
}
$link= 'http://www.targetpage.php';
echo wrapWithAnchor($link,$demoString1);
echo '<hr />';
echo wrapWithAnchor($link,$demoString2);
?>
I have a regex code which finds all URLs and replaces them with a HTML link. Here is my code:
// initializing
$str = "this is a good website www.example.com/classname/methodname/arg";
$rexProtocol = '(https?://)?';
$rexDomain = '((?:[-a-zA-Z0-9]{1,63}\.)+[-a-zA-Z0-9]{2,63}|(?:[0-9]{1,3}\.){3}[0-9]{1,3})';
$rexPort = '(:[0-9]{1,5})?';
$rexPath = '(/[!$-/0-9:;=#_\':;!a-zA-Z\x7f-\xff]*?)?';
$rexQuery = '(\?[!$-/0-9:;=#_\':;!a-zA-Z\x7f-\xff]+?)?';
$rexFragment = '(#[!$-/0-9:;=#_\':;!a-zA-Z\x7f-\xff]+?)?';
function callback($match){
// Prepend http:// if no protocol specified
$completeUrl = $match[1] ? $match[0] : "http://{$match[0]}";
$DetectProperName = strlen($match[2].$match[3].$match[4]) > 20 ? "...".substr($match[2].$match[3].$match[4],0,20) : $match[2].$match[3].$match[4];
return ''.$DetectProperName. '';
}
echo $str = preg_replace_callback("&\\b$rexProtocol$rexDomain$rexPort$rexPath$rexQuery$rexFragment(?=[?.!,;:\"]?(\s|$))&",'callback', htmlspecialchars($str));
Also here is the output:
this is a good website ...www.example.com/clas
Also here is a fiddle
Well, that's ok and it works as well for links. Now my question is about when input is containing a quote ' or ". That regex will add a \ next to it. How can I fix it? I want such a regex be not sensitive to quotes.
Here is an example:
Input:
$str = 'this is a " (quote)';
Current Output:
this is a \" (quote)
What I want:
this is a " (quote)
How can I do that?
Edit: According to some tests, I figured out that change single/double quotes to ASKII code. How can I prevent it?
I'm trying to echo a phpstring-message. This php string consists of html and php variables and comes form a database and i can't change that data.
$name = 'John';
$str = '<b>Hi {$name},</b><br/>How are you?';
echo $str;
So i'm trying to replace the php string, but it doesn't work. This is my code:
$str = str_replace('{', '\' . ', $str);
$str = str_replace('}', ' . \' ', $str);
I get: <b>Hi' . $name. ',</b><br/>How are you?
How do i get the string like this?
<b>Hi John,</b><br/>How are you?
Thank you in advance
Just do it like this, you won't be able to replace it with a concatenation:
echo str_replace('{$name}', $name, $str);
EDIT:
If you don't know the name of the variable just use this:
echo preg_replace('/\{(.*?)\}/', $name, $str);
it's already implemented in PHP, you can directly write the variable in double quote like this:
echo "<b>Hi $name,</b><br/>How are you?";
or for some more complex variables:
echo "<b>Hi {$user->name},</b><br/>How are you?";
I am using Iconv function to convert string to requested character encoding. Look On Below Code
$sms_text = 'A:'f3*'F'; // Output received from SMPP
$result = iconv('UTF-16BE' ,'UTF-8//IGNORE' , $sms_text);
echo 'Ignore: ' .$result;
echo $sms_text = iconv('UTF-16BE' ,'UTF-8' , $sms_text);
$result1 = iconv('UTF-16BE' ,'UTF-8//TRANSLIT' , $sms_text); //line no (53)
echo 'Transilt: '.$result1;
And I received the below Output
If I have a string of dari and pashto Language, then its showing only first word and dont return remaining string after blank space. Even //IGNORE gives the same output.
Should I replace these blank spaces with the help of with some other character so that I can get complete string?
Note: I am passing string received from SMPP(receiver).
SMS Sent to SMPP : افغانستان کابل
Outpur Received from SMPP : 'A:'f3*'F
String back converted by iconv : افغانستان
English string is working well.
Thanks in advance.
You are converting string to UTF-8 charset on this line:
echo $sms_text = iconv('UTF-16BE' ,'UTF-8' , $sms_text);
The error appears becouse you are trying to convert it second time.
To resolve this issue you should not update $sms_text variable on mentioned line.
Here is a code and output that works for me:
$sms_text ="فغانستان کابل";
echo "UTF-8 : $sms_text \n";
$sms_text = iconv('UTF-8', 'UTF-16BE', $sms_text);
echo "UTF-16BE : $sms_text \n";
echo 'Ignore: ' . iconv('UTF-16BE' ,'UTF-8//IGNORE' , $sms_text) . "\n";
echo 'Simple: ' . iconv('UTF-16BE' ,'UTF-8' , $sms_text) . "\n";
echo 'Transilt: '. iconv('UTF-16BE' ,'UTF-8//TRANSLIT' , $sms_text) . "\n";
Output:
UTF-8 : فغانستان کابل
UTF-16BE : A:'F3*'F
Ignore: فغانستان کابل
Simple: فغانستان کابل
Transilt: فغانستان کابل
As for blank spaces, could you please share the test string?