This question already has answers here:
How to split a right-to-left string in php
(2 answers)
arabic fonts display in reverse order in dompdf
(2 answers)
Convert a String into an Array of Characters - multi-byte
(2 answers)
Closed last month.
i need to split Arabic text am trying different methods but not solved it
$input = "ابجد";
$split = mb_str_split($input);
print_r($split);
am expected output like
(ا،ب،ج،د)
need solution
You can try mb_str_split() which can process multibyte strings. This code:
$input = "ابجد";
$split = mb_str_split($input);
print_r($split);
results in:
Array
(
[0] => ا
[1] => ب
[2] => ج
[3] => د
)
When you manipulate (trim, split, splice, etc.) strings encoded in a multibyte encoding, you need to use special functions since two or more consecutive bytes may represent a single character in such encoding schemes. Otherwise, if you apply a non-multibyte-aware string function to the string, it probably fails to detect the beginning or ending of the multibyte character and ends up with a corrupted garbage string that most likely loses its original meaning.
You can reverse the output by using array_reverse(), like this:
$input = "ابجد";
$split = mb_str_split($input);
$reversed = array_reverse($split);
print_r($reversed);
The output now is:
Array
(
[0] => د
[1] => ج
[2] => ب
[3] => ا
)
See: https://3v4l.org/ZsJNb
You can try implode for output=(ا،ب،ج،د) :
$input = "ابجد";
$split = mb_str_split($input,1,'UTF-8');
print "(".implode('،',$split).")";
//output: (ا،ب،ج،د)
Related
This question already has answers here:
Split string on spaces except words in quotes
(4 answers)
Closed 3 years ago.
I'm building a website using PHP.
I am using a preg_split() to separate a given string which looks like +word1 -word2 -"word word".
But I need them in the following form +word1, -word2, -"word word".
Currently, I have this one:
$words = preg_split("/[\s\"]*\"([^\"]+)\"[\s\"]*|" . "[\s\"]*'([^']+)'[\s\"]*|" . "[\s\"]+/", $search_expression, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
but it didn't work as I wish: I need to do it in this way to get it work:
+word1 -word2 '-"word word"'.
Does someone have a better regex or idea?
One option is to match from a double quote till a double quote and don't split on that match using SKIP FAIL. Then match 1+ horizontal whitespace chars to split on.
"[^"]+"(*SKIP)(*FAIL)|\h+
Regex demo | Php demo
For example
$search_expression = '+word1 -word2 -"word word"';
$words = preg_split("~\"[^\"]+\"(*SKIP)(*FAIL)|\h+~", $search_expression);
print_r($words);
Output
Array
(
[0] => +word1
[1] => -word2
[2] => -"word word"
)
A simpler expression with greedy ? works for matching your examples:
preg_match_all('/[+-][^+-]+ ?/', $search_expression, $matches);
print_r($matches[0]);
Yields:
Array
(
[0] => +word1
[1] => -word2
[2] => -"word word"
)
Se Example.
This question already has answers here:
Convert a String into an Array of Characters - multi-byte
(2 answers)
Closed 3 years ago.
Tell me, please, how to split a string character by character into an array, but so that the stresses (for example, one) in the array do not lie as a separate character, but remain with a letter?
Now it works like this, but stresses are considered a separate character.
$ arrayLetters = preg_split ('// u', $ string, NULL, PREG_SPLIT_NO_EMPTY);
Hope is's help:
$str = 'PHP';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
Output:
Array
(
[0] => P
[1] => H
[2] => P
)
Currently I am trying to split the � the special character which represents %A0 at the URL. However when I use another URL, it doesn't recognize %A0 therefore I need to use %20 which is the standard space.
My question is. Is there a way to explode() special character �? Whenever I try to explode, it always return a single index array with length 1 array.
//Tried str_replace() to replace %A0 to empty string. Didn't work
$a = str_replace("%A0"," ", $_GET['view']);
// Tried to explode() but still returning single element
$b = explode("�", $a);
// Returning Array[0] => "Hello World" insteand of
// Array[2] => [0] => "Hello", [1] => "World"
echo $b[0];
Take a look at mb_split:
array mb_split ( string $pattern , string $string [, int $limit = -1 ] )
Split a multibyte string using regular expression pattern and returns
the result as an array.
Like this:
$string = "a�b�k�e";
$chunks = mb_split("�", $string);
print_r($chunks);
Outputs:
Array
(
[0] => a
[1] => b
[2] => k
[3] => e
)
This question already has answers here:
Extract words from string with preg_match_all
(7 answers)
Closed 8 years ago.
I need to extract the names from the following string:
$contact = "John96783819Dickson97863424"
i tried using this:
preg_match('/[a-zA-Z]/',$contact,$matches);
but i get an array with all the alphabets individually in the array.
Desired Output:
Array ([0] => 'John', [2] => 'Dickson')
And now it gets complicated. The same reggae should extract this
$contact = 'Vincent Tan96123179Lawrence Thoo90603123Ryan Ong91235721'
into this
Array ([0] => 'Vincent Tan', [2] => 'Lawrance Thoo' , [3] => 'Ryan Ong')
How do i do that?
All you need is to quantify the character class using +
/[a-zA-Z]+/
+ matches one occurence of presceding regex
Example : http://regex101.com/r/bI6aH1/1
preg_match_all('/[a-zA-Z]+/',$contact,$matches);
Will give output as
Array ( [0] => John [1] => Dickson )
preg_match('/[a-zA-Z]+/',$contact,$matches);
The /[a-zA-Z]/ means match any ONE letter, anywhere in the string. Adding the + in /[a-zA-Z]+/ means match one or MORE sequential letters.
This question already has answers here:
php explode all characters [duplicate]
(4 answers)
Closed 8 years ago.
how i can explode string into an array. Acctually i want to translate english language into the braille. First thing i need to do is to get the character one by one from a string,then convert them by mathing the char from value in database and display the braille code using the pic. As example when user enter "abc ef", this will create value separately between each other.
Array
(
[0] => a
[1] => b
[2] => c
[3] =>
[4] => e
[5] => f
)
i tried to do but not get the desired output. This code separated entire string.
$papar = preg_split('/[\s]+/', $data);
print_r($papar);
I'm sorry for simple question,and if you all have an idea how i should do to translate it, feel free to help. :)
If you're using PHP5, str_split will do precisely what you're seeking to accomplish. It splits each character in a string – including spaces – into an individual element, then returns an array of those elements:
$array = str_split("abc ef");
print_r($array);
Outputs the following:
Array
(
[0] => a
[1] => b
[2] => c
[3] =>
[4] => e
[5] => f
)
UPDATE:
The PHP4 solution is to use preg_split. Pass a double forward slash as the matching pattern to delimit the split character-by-character. Then, set the PREG_SPLIT_NO_EMPTY flag to ensure that no empty pieces will be returned at the start and end of the string:
$array = preg_split('//', "abc ef", -1, PREG_SPLIT_NO_EMPTY); // Returns same array as above
PHP has a very simple method of getting the character at a specific index.
To do this, utilize the substring method and pass in the index you are looking for.
How I would approach your problem is
Ensure the string you are converting has at least a size of one
Determine how long the string is
int strlen ( string $string )
Loop over each character and do some operation depending on that character
With a string "abcdef"
$rest = substr("abcdef", -1); // returns "f"
See the substr page for a full example and more information
http://us2.php.net/substr
You can iterate the string itself.
$data = 'something string';
for($ctr = 0; $ctr < strlen($data); $ctr++)
{
if($data{$ctr}):
$kk = mysql_query("select braille_teks from braille where id_huruf = '$data{$ctr}'");
$jj = mysql_fetch_array($kk);
$gambar = $jj['braille_teks'];
?>
<img src="images/<?php echo $gambar;?>">
<?php
else:
echo " ";
}