This question already has answers here:
PHP: How can I explode a string by commas, but not wheres the commas are within quotes?
(2 answers)
Closed 8 years ago.
I'm trying to explode a string with commas, but in the string are some commas I don't want to get explode, those commas are between " " like: "hey, how are you". I tried multiple things like exploding the " first. So how can I do this?
For example i have this string:
hey,how,are,"yo how,are you?",i'm,good
So the output will be:
array (
0 => hey,
1 => how,
2 => are,
3 => "you how, are you?",
4 => i'm,
5 => good
);
I tried to explode first on " that but I have no idea how.
Use str_getcsv() for PHP >= 5.3:
var_dump( str_getcsv( 'hey,how,are,"yo how,are you?"'));
This prints:
array(4) { [0]=> string(3) "hey" [1]=> string(3) "how" [2]=> string(3) "are" [3]=> string(15) "yo how,are you?" }
Related
I have text:
<b>Title1:</b><br/><b>Title2:</b> Value1<br/><b>Title3:</b> Value2<br/><b>Title4:</b> Value3<br/>Value4<b>Title5:</b> Value5<br/>
What regex to get:
[0] => <b>Title1:</b><br/>
[1] => <b>Title2:</b> Value1<br/>
[2] => <b>Title3:</b> Value2<br/>
[3] => <b>Title4:</b> Value3<br/>Value4
[4] => <b>Title5:</b> Value5<br/>
My variant not working:
<b>(.*?)</b>(.*?)
You can use preg_split() with a lookahead:
<?php
$split = preg_split( '/(?=<b>Title\d+:)/', '<b>Title1:</b><br/><b>Title2:</b> Value1<br/><b>Title3:</b> Value2<br/><b>Title4:</b> Value3<br/>Value4<b>Title5:</b> Value5<br/>' );
array_shift( $split );
var_dump( $split );
Output:
array(5) {
[0]=>
string(19) "<b>Title1:</b><br/>"
[1]=>
string(26) "<b>Title2:</b> Value1<br/>"
[2]=>
string(26) "<b>Title3:</b> Value2<br/>"
[3]=>
string(32) "<b>Title4:</b> Value3<br/>Value4"
[4]=>
string(26) "<b>Title5:</b> Value5<br/>"
}
Your regex was close, you need:
<b>(.*?)<\/b>(.*?)(?=<b>|$)
https://regex101.com/r/dk67IK/1
A resource like this can be very useful in troubleshooting regex: https://regex101.com/
Looks like you are missing an escape character in <b>(.*?)</b>(.*?)
<b>(.*?)<\/b>(.*?) should stop an error from being thrown for that current regex and get you close to the result, you'll need to work with it a bit more to get the exact results you want though.
<b>(.*?)<\/b>(.*?)<br\/> should be a bit closer I think as it looks like you want to include the break tags.
i have data in my mysql database "Service,House Worker" (without ""), when i access my data from the database i found (Service,House Worker) as it is, when i try to convert with (var_dump(explode(',',($CheckBoxAnswer)));) it then its return following :
array(1) { [0]=> string(26) "Service,House Worker" }
but i want something similar:
array(1)
(
[0] => string(7) "Service"
[1] => string(13) "House Worker"
)
$CheckBoxAnswer is contain data i pulled from mysql.
i tried with var_dump(explode(',',($CheckBoxAnswer)));
but its not working
Try this:
var_dump(explode(',',($CheckBoxAnswer[0])));
You are tring to explode an array into an another array. You need to specify the string instead.
First you need to trim $CheckBoxAnswer
Because by using var_dump giving string length 26 instead of 20
Then after use string replace
str_replace('/','',$CheckBoxAnswer);
After that try to use var_dump(explode(',',($CheckBoxAnswer)));
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 6 years ago.
I have the following array of arrays:
array(1) {
[0]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(3) "abc"
[1]=>
string(3) "įāē"
}
[1]=>
array(2) {
[0]=>
string(3) "čaē"
[1]=>
string(3) "qwe"
}
}
}
I am using the bellow code to echo the result on a page:
echo json_encode($array);
I get the following result on my page:
[[["abc",null],[null,"qwe"]]]
Every string with special char is converted to null.
So I´ve tried utf8_encode on each of the element in the array:
foreach($array as &$subarray1){
foreach($subarray1 as &$subarray2){
foreach($subarray2 as &$subarray3){
$subarray3 = utf8_encode($subarray3);
}
}
}
But I get the following result:
[[["abc","\u00e1\u00e2\u00e7"],["\u00e8a\u00e7","qwe"]]]
What is the proper way to encode this?
json_encode supports a second parameter so you can use the constant JSON_UNESCAPED_UNICODE like the following:
$arr = [
0 => [0 => "abc", 1 => "įāē"],
1 => [0 => "čaē", 1 => "qwe"]
];
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
You can find a working demo here: https://ideone.com/J5bvT5
This question already has an answer here:
PHP preg_match to find multiple occurrences
(1 answer)
Closed 8 years ago.
I'm weak with regex, need help. My problem is I have to extract all the string that matches the given pattern I have into an array. See the problem below:
The string
<?php
$alert_types = array(
'warning' => array('', __l("Warning!") ),
'error' => array('alert-error', __l("Error!") ),
'success' => array('alert-success', __l("Success!") ),
'info' => array('alert-info', __l("For your information.") ),
);?>
The Preg_Match Code
preg_match("/.*[_][_][l][\(]['\"](.*)['\"][\)].*/", $content, $matches);
I'm only getting the first one match which is Warning!. I'm Expecting matches will have the following values:
Warning!, Error!, Success!, For your information.
Actually I'm using file_get_contents($file) to get the string.
Can anyone help me to solve this. Thankyou in advance.
preg_match() only finds the first match in the string. Use preg_match_all() to get all matches.
preg_match_all("/.*__l\(['\"](.*?)['\"]\).*/", $content, $matches);
$matches[1] will contain an array of the strings you're looking for.
BTW, you don't need all those single-character brackets. Just put the character into the regexp.
var_dump($matches);
array(2) {
[0]=>
array(4) {
[0]=>
string(45) " 'warning' => array('', __l("Warning!") ),"
[1]=>
string(52) " 'error' => array('alert-error', __l("Error!") ),"
[2]=>
string(58) " 'success' => array('alert-success', __l("Success!") ),"
[3]=>
string(65) " 'info' => array('alert-info', __l("For your information.") ),"
}
[1]=>
array(4) {
[0]=>
string(8) "Warning!"
[1]=>
string(6) "Error!"
[2]=>
string(8) "Success!"
[3]=>
string(21) "For your information."
}
}
I'm having problems matching the[*] which is sometimes there and sometimes not. Anyone have suggestions?
$name = 'hello $this->row[today1][] dfh fgh df $this->row[test1] ,how good $this->row[test2][] is $this->row[today2][*] is monday';
echo $name."\n";
preg_match_all( '/\$this->row[.*?][*]/', $name, $match );
var_dump( $match );
output:
hello $this->row[test] ,how good $this->row[test2] is $this->row[today][*] is monday
array (
0 =>
array (
0 => '$this->row[today1][*]',
1 => '$this->row[test1] ,how good $this->row[test2][*]',
2 => '$this->row[today2][*]',
),
)
Now the [0][1] match takes on too much because it is matching until the next '[]' instead of ending at '$this->row[test]' . I'm guessing the [*]/ adds a wildcard. Somehow need to check if the next character is [ before matching to []. Anyone?
Thanks
[, ] and * are special meta characters in regex and you need to escape them. Also you need to make last [] optional as per your question.
Following these suggestions following should work:
$name = 'hello $this->row[today1][] dfh fgh df $this->row[test1] ,how good $this->row[test2][] is $this->row[today2][*] is monday';
echo $name."\n";
preg_match_all( '/\$this->row\[.*?\](?:\[.*?\])?/', $name, $match );
var_dump( $match );
OUTPUT:
array(1) {
[0]=>
array(4) {
[0]=>
string(20) "$this->row[today1][]"
[1]=>
string(17) "$this->row[test1]"
[2]=>
string(19) "$this->row[test2][]"
[3]=>
string(21) "$this->row[today2][*]"
}
}