I have the following regular expression callback, and the issue that I am having, is that in the final result, the ' are removed. What can I do to prevent this from happening?
<?php
$eval = "'$server.REQUEST_URI' == '/user/photos'";
$result = preg_replace_callback('/\'(.+)\'/isU', function($matches){
$match = $matches[1];
$find = Object69::find($match);
return !empty($find) ? $find : $match;
}, $eval);
var_dump($result);
here is the result of $result
string(29) " /user/photos == /user/photos"
This is what I was expecting:
string(29) "'/user/photos' == '/user/photos'"
and here are the arrays that it finds $matches:
array(2) {
[0]=>
string(21) "'$server.REQUEST_URI'"
[1]=>
string(19) "$server.REQUEST_URI"
}
array(2) {
[0]=>
string(14) "'/user/photos'"
[1]=>
string(12) "/user/photos"
}
Related
I'm trying to convert a string into a multidimentional array.
I've found many answers online but they expect you to have array keys etc...
My String:
UserIds\n234234\n20053\n19928\n16325
I've tried the usual way:
$arr= array();
$arr = explode("\n", $string);
and i've also tried
$arr[] = explode("\n", $string);
but the result is always like this:
array(5) {
[0]=> string(7) "UserIds"
[1]=> string(6) "234234"
[2]=> string(5) "20053"
[3]=> string(5) "19928"
[4]=> string(5) "16325"
}
My Expected Result:
array(5) {
[0]=> array(1) { [0]=> string(7) "UserIds" }
[1]=> array(1) { [0]=> int(234234) }
[2]=> array(1) { [0]=> int(20053) }
[3]=> array(1) { [0]=> int(19928) }
[4]=> array(1) { [0]=> int(16325) }
}
One way to do it could be to map the response using array_map and wrap the items in an array:
$string = "UserIds\n234234\n20053\n19928\n16325";
$arr = array_map(function($x){return [$x];}, explode("\n", $string));
print_r($arr);
Demo
How add a link using the id on comma separated multidimensional array?
What I done so far:
$string = implode(", ", array_column($title_genres, "name"));
echo $string;
My array:
array(4) {
[0]=>
object(stdClass)#66 (3) {
["id"]=>
string(2) "21"
["name"]=>
string(8) "Aventure"
["tmdb_id"]=>
string(2) "12"
}
[1]=>
object(stdClass)#67 (3) {
["id"]=>
string(2) "20"
["name"]=>
string(9) "Animation"
["tmdb_id"]=>
string(2) "16"
}
[2]=>
object(stdClass)#63 (3) {
["id"]=>
string(1) "8"
["name"]=>
string(8) "Familial"
["tmdb_id"]=>
string(5) "10751"
}
[3]=>
object(stdClass)#70 (3) {
["id"]=>
string(1) "9"
["name"]=>
string(11) "Fantastique"
["tmdb_id"]=>
string(2) "14"
}
}
I'd suggest a simple foreach on $title_genres to build a new array of just linked names, then you can do the implode on that to output with commas:
$links = array();
foreach($title_genres as $item) {
$links[] = ''. $item['name'] .'';
}
echo implode(', ',$links);
Update 1: In case you are using stdClass objects:
$links = array();
foreach($title_genres as $item) {
$links[] = ''. $item->name .'';
}
echo implode(', ',$links);
Update 2: An all in one line mash using array_map ;)
echo implode(', ', array_map(function($a){
return ''. $a->name .'';
},$title_genres) );
Here's the array_map solution, you decide if you consider it simpler than foreach.
$string = implode(",", array_map(function($item) {
return "<a href='/some.php?id={$item->id'}'>{$item->name}</a>";
}, $title_genres));
echo $string;
If the base URL is in a variable, you can access it using a use() declaration.
$baseurl = base_url();
$string = implode(",", array_map(function($item) use ($baseurl) {
return "<a href='$baseurl/some.php?id={$item->id'}'>{$item->name}</a>";
}, $title_genres));
Kinda of a noobie in PHP and Regex, I receive the following from a web service:
test:002005#1111#333333#;10205#2000#666666#;002005#1111#55555#;
The above line is a sequence of 3 numbers which repeats 3 times. I would like to get the 3rd number of each sequence and I believe the best course (besides 3000 explodes) would be preg_match_all but I am having a tough time wrapping my mind around RegEx.
The end result should look like this:
Array
(
[0] => 333333
[1] => 666666
[2] => 55555
)
Thanks in advance for any help.
if(preg_match_all('/.*?(?:\d+#){2}(\d+)#;/',$s,$m)) {
print_r($m[1]);
}
http://ideone.com/99M9t
or
You can do it using explode as:
$input = rtrim($input,';');
$temp1 = explode(';',$input);
foreach($temp1 as $val1) {
$temp2 = explode('#',$val1);
$result[] = $temp2[2];
}
print_r($result);
http://ideone.com/VH29g
Use the function explode()
<?php
$pizza = "piece1#piece2#piece3#piece4#piece5#piece6";
$pieces = explode("#", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
?>
I don't remember exactly how the saying goes but...
"You have a problem and decide to use regular expressions... now you have two problems."
Your problem can easily be solved if we assume 'test:' isn't part of the actual string to be parsed.
<?php
$in = '002005#1111#333333#;10205#2000#666666#;002005#1111#55555#;';
function splitGroupsAndGetColumn($input, $groupSeparator, $columnSeparator, $columnIndex, $skipEmpty=true)
{
$result = array();
$groups = explode($groupSeparator, $input);
foreach($groups as $group)
{
$columns = explode($columnSeparator, $group);
if (isset($columns[$columnIndex]))
{
array_push($result, $columns[$columnIndex]);
}
else if (! $skipEmpty)
{
array_push($result, NULL);
}
}
return $result;
}
var_dump(splitGroupsAndGetColumn($in, ';', '#', 2));
Output:
array(3) {
[0]=>
string(6) "333333"
[1]=>
string(6) "666666"
[2]=>
string(5) "55555"
}
You could use preg_match_all for this task, which makes the task quite simple:
$a = "test:002005#1111#333333#;10205#2000#666666#;002005#1111#55555#;";
preg_match_all('/#(\d+)#;/', $a, $m);
print_r($m);
$m[1] contains the output, you want.
Reference: http://php.net/manual/en/function.preg-match-all.php
My version :)
The regex (\d+) means I want all that is a number one or more
php > $a = '002005#1111#333333#;10205#2000#666666#;002005#1111#55555#';
php > preg_match_all('/(\d+)/',$a,$matches);
php > var_dump($matches);
array(2) {
[0]=>
array(9) {
[0]=>
string(6) "002005"
[1]=>
string(4) "1111"
[2]=>
string(6) "333333"
[3]=>
string(5) "10205"
[4]=>
string(4) "2000"
[5]=>
string(6) "666666"
[6]=>
string(6) "002005"
[7]=>
string(4) "1111"
[8]=>
string(5) "55555"
}
[1]=>
array(9) {
[0]=>
string(6) "002005"
[1]=>
string(4) "1111"
[2]=>
string(6) "333333"
[3]=>
string(5) "10205"
[4]=>
string(4) "2000"
[5]=>
string(6) "666666"
[6]=>
string(6) "002005"
[7]=>
string(4) "1111"
[8]=>
string(5) "55555"
}
}
Given:
$val = "font-size:12px;color:#ff0000;font-family:Arial";
The following code will explode the string twice, to produce an array of arrays:
$val = explode(';',$val);
foreach($val as &$v)
$v = explode(':',$v);
var_dump($val);
The output is:
array(3) {
[0]=>
array(2) {
[0]=>
string(9) "font-size"
[1]=>
string(4) "12px"
}
[1]=>
array(2) {
[0]=>
string(4) "fill"
[1]=>
string(7) "#ff0000"
}
[2]=>
&array(2) {
[0]=>
string(11) "font-family"
[1]=>
string(5) "Arial"
}
}
Is there a more efficient / cleaner way to achieve the same result?
I'd prefer something with no lambda functions since PHP 5.2 doesn't support them. But this is a purely intellectual question anyway, so, that's just a preference.
You can try with:
$input = "font-size:12px;color:#ff0000;font-family:Arial";
preg_match_all('/([^:]*?):([^;]*);?/', $input, $matches);
$output = array_combine($matches[1], $matches[2]);
Output:
array(3) {
["font-size"]=>
string(4) "12px"
["color"]=>
string(7) "#ff0000"
["font-family"]=>
string(5) "Arial"
}
I'd recommend against references--you can run into some odd errors. But your approach is fine. Alternatively, you could do something with array_map:
$val = array_map(function($v) { return explode(':', $v); }, explode(';', $val)));
I have a problem with preg_match_all.
While preg_match does reply the whole match as the first element of the array, preg_match_all doesn't - the first array is empty.
At least with the pattern I chose (havn't tried others since it's the one I need) it doesn't work.
Here is my code:
preg_match_all("/<\?\?(\t| )?translate(\t| )?;(\t| )?(.*)(\t| )?\?\?>/U", $file, $translate_info);
The pattern itself is working and producing subpattern matches.
Updated according to new given details :
$file = '<?? translate ; foo bar??>';
$res = preg_match_all('/<\?\?(\t| )?translate(\t| )?;(\t| )?(.*)(\t| )?\?\?>/U', $file, $translate_info);
echo "res='$res'\n";
var_dump($translate_info);
Works for me, it gives :
res='1'
array(6) {
[0]=>
array(1) {
[0]=>
string(26) "<?? translate ; foo bar??>"
}
[1]=>
array(1) {
[0]=>
string(1) " "
}
[2]=>
array(1) {
[0]=>
string(1) " "
}
[3]=>
array(1) {
[0]=>
string(0) ""
}
[4]=>
array(1) {
[0]=>
string(8) " foo bar"
}
[5]=>
array(1) {
[0]=>
string(0) ""
}
}