How to separate exact characters based on accents? [duplicate] - php

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
)

Related

unable to split Arabic Alphabets [duplicate]

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: (ا،ب،ج،د)

preg_split to seperate input [duplicate]

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.

PHP preg_split between Character (prefix) and Number [duplicate]

This question already has answers here:
preg_split() String into Text and Numbers [duplicate]
(2 answers)
Closed 4 years ago.
Sorry for my bad English, I would to ask about How to split characters and numbers using preg_split(). For example, I have any data with prefix like :
ABC00001 to array(0 => 'ABC', 1 => 00001)
DEFG00002 to array(0 => 'DEFG', 1 => 00002)
AB00003 to array(0 => 'AB', 1 => 00003)
Thanks for advise
Split on the zero-length position that follows the sequence of letters.
Code: Demo
$string = 'ABC00001';
$output = preg_split('~[A-Z]+\K~', $string);
var_export($output);
The \K says forget the previous matched characters.
You can use preg_split with the PREG_SPLIT_DELIM_CAPTURE and PREG_SPLIT_NO_EMPTY flags to do what you want:
$string = 'ABC00001';
$output = preg_split('/([A-Z]+)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($output);
Output:
Array ( [0] => ABC [1] => 00001 )
Demo on 3v4l.org

preg_split every character, but don't split if is quote [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I'm using the following code to split my UTF-8 strings to characters:
$characters = preg_split('//u', $word, -1, PREG_SPLIT_NO_EMPTY);
In some cases, a character might have a single quote after it. for example: hel'lo. I want to keep that quote with the character before it.
Using the regex above, my array is this:
Array
(
[0] => h
[1] => e
[2] => l
[3] => '
[4] => l
[5] => o
)
And I want the array to be:
Array
(
[0] => h
[1] => e
[2] => l'
[3] => l
[4] => o
)
How can I do it?
Thanks!
(the single quote can be at the beginning of the string, at the end of it and in the middle of it).
Rather than split, you can do preg_match_all using
'?\p{L}'?
i.e. an optional ' before and after the Unicode letter:
preg_match_all("/'?\\p{L}'?/u", $str, $matches);
RegEx Demo
Use ! to prevent from split
$characters = preg_split("/(?!')/u", $word, -1, PREG_SPLIT_NO_EMPTY);

explode string one by one character including space [duplicate]

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 "&nbsp";
}

Categories