Basically what I am trying to do is add text before (and after) each element of an array. This is an example:
<?php
$word="code";
$chars=preg_split('//', $word, -1, PREG_SPLIT_NO_EMPTY);
print"<pre>";
print_r($chars);
print"</pre>";
?>
(Yes I need the regex so I can't just use str_split())
which outputs:
Array
(
[0] => c
[1] => o
[2] => d
[3] => e
)
Now my ultimate goal is to get the final string to be something like:
"shift+c","shift+o","shift+d","shift+e"
If I can get help just adding the "shift+ in front of each element, then I can use implode() to do the rest.
Here's a solution based on my comments:
$word = 'code';
$result = array_map(function($c){ return "shift+$c"; }, str_split($word));
And here's the output var_dump($result):
array(4) {
[0]=> string(7) "shift+c"
[1]=> string(7) "shift+o"
[2]=> string(7) "shift+d"
[3]=> string(7) "shift+e"
}
Edit: If you really need to you can use the result from preg_split as the array in array_map.
You can loop through the chars array and concatenate your desired string.
<?php
$word="code";
$chars=preg_split('//', $word, -1, PREG_SPLIT_NO_EMPTY);
foreach($chars as $c){
echo "shift+" . $c . " ";
}
?>
Outputs:
shift+c shift+o shift+d shift+e
Related
array(3) {
[20]=>
string(43) "{"shortname":"testvqweq","fullname":"test"}"
[21]=>
string(51) "{"shortname":"bwqbdwqbwqeb","fullname":"qwbdwqbwq"}"
[22]=>
string(48) "{"shortname":"wqdwqdwqdw","fullname":"dwqwqdwq"}"
}
I want to access shortname, fullname from array like:
Testvqweqe test
Here is an example that will clear things up for you I hope:
<?php
$array = [
"20" => '{"shortname":"testvqweq","fullname":"test"}',
"21" => '{"shortname":"bwqbdwqbwqeb","fullname":"qwbdwqbwq"}',
"22" => '{"shortname":"wqdwqdwqdw","fullname":"dwqwqdwq"}',
];
echo '<pre>';
print_r($array);
foreach($array as $json){
echo '<pre>';
$j2a = json_decode($json,true);
print_r($j2a['shortname']);
}
$j2a1 = json_decode($array[20],true)['fullname']; /*grab the value in the same line as the json_decode */
echo '<pre>';
echo "j2a1: ";
print_r($j2a1); /* Could have used an echo :) */
Will return:
Array
(
[20] => {"shortname":"testvqweq","fullname":"test"}
[21] => {"shortname":"bwqbdwqbwqeb","fullname":"qwbdwqbwq"}
[22] => {"shortname":"wqdwqdwqdw","fullname":"dwqwqdwq"}
)
testvqweq
bwqbdwqbwqeb
wqdwqdwqdw
j2a1: test
I dumbed it down on purpose and did not use one lines to show how you turn the json in the array into an array and print the value you like from that one... and at the end I gave an example of a one liner without a loop.
You could simple access them with a foreach
foreach(array as arrayIndex)
{
$shortName = arrayIndex['shortname'];
$fullName= arrayIndex['shortname'];
}
if you need to use the variables you will want to do that action within the foreach loop as well or alternatively you will want to store them outside the foreach loop.
GL.
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 am using preg_split to split a string into words.
However, it is not working for a particular string that is fetched from a mysql text column.
If I manually assign the string to a variable it will work correctly but not when the string is fetched from the database.
Here is the simple code I am using:
//The failing string. When manually assigned like this it works correctly
$string = "<p><strong>Iden is lesz lehetoseg a foproba és a koncert napjan ebedet kerni a MUPA-ban. Ára 1000-1200 Ft körül várható. Azoknak, akik még nem jártak a MUPA-ban ingyenes bejarasi lehetoseget biztositunk. Tovabba segitunk a pesti szallas megszervezeseben is, ha igenyt tartotok ra.</strong></p>";
$string = strip_tags(trim($string));
$words = preg_split('/\PL+/u', $string, null, PREG_SPLIT_NO_EMPTY);
Here is what the preg_split returns when called on the string from the database:
array(1) { [0]=> string(269) "Iden is lesz lehetoseg a foproba és a koncert napjan ebedet kerni a MUPA-ban. Ára 1000-1200 Ft körül várható. Azoknak, akik még nem jártak a MUPA-ban ingyenes bejarasi lehetoseget biztositunk. Tovabba segitunk a pesti szallas megszervezeseben is, ha igenyt tartotok ra." }
Does anyone know what is causing preg_split to fail for this string?
Thanks
I tested your code with a string from the database and happened the same error, change the regular expresion and you will have the solution. Use this expression:
$words = preg_split('/[\s]/', $string, null, PREG_SPLIT_NO_EMPTY);
//var_dump result
array(42) {
[0]=>
string(4) "Iden"
[1]=>
string(2) "is"
[2]=>
string(4) "lesz"
[3]=>
string(9) "lehetoseg"
...
}
UPDATE:
The modifier /u are for UTF 8, maybe your database is not in UTF8, and so the expression did not work
You don't need a regex for this, explode will do the job:
$string = "<p><strong>Iden is lesz lehetoseg a foproba és a koncert napjan ebedet kerni a MUPA-ban. Ára 1000-1200 Ft körül várható. Azoknak, akik még nem jártak a MUPA-ban ingyenes bejarasi lehetoseget biztositunk. Tovabba segitunk a pesti szallas megszervezeseben is, ha igenyt tartotok ra.</strong></p>";
$string = strip_tags(trim($string));
$words = explode(" ", $string);
print_r($words);
Output:
Array
(
[0] => Iden
[1] => is
[2] => lesz
[3] => lehetoseg
[4] => a
[5] => foproba
[6] => és
[7] => a
[8] => koncert
...
Ideone Demo
I have a relatively large array of elements which I want to search for a string and replace any matches. I'm currently trying to do this using preg_replace and regular expressions:
preg_replace("/\d?\dIPT\.\w/", "IPT", $array);
I want to get all values which match either 00IPT.A or 0IPT.A (with 0 representing any numerical character and A representing any letter) and replace them with IPT. However, I'm getting array to string conversion notices. Is there any way to get preg_replace to accept an array data source? If not, is there any other way I could achieve this?
The documentation says that preg_replace should be able to accept array sources — this is the reason I'm asking.
The string or an array with strings to search and replace.
If subject is an array, then the search and replace is performed on every entry of subject, and the return value is an array as well.
The array is multidimensional if that helps (has multiple arrays under one main array).
preg_replace doesn't modify in place. To permanently modify $array, you simply need to assign the result of preg_replace to it:
$array = preg_replace("/\d{1,2}IPT\.\w/", "IPT", $array);
works for me.
$array = array('00IPT.A', '0IPT.A');
$array = preg_replace("/\d{1,2}IPT\.\w/", "IPT", $array);
var_dump($array);
// output: array(2) { [0]=> string(3) "IPT" [1]=> string(3) "IPT" }
Note: the \d{1,2} means one or two digits.
If you want to do this to a two-dimensional array, you need to loop through the first dimension:
$array = array( array('00IPT.A', 'notmatch'), array('neither', '0IPT.A') );
foreach ($array as &$row) {
$row = preg_replace("/\d{1,2}IPT\.\w/", "IPT", $row);
}
var_dump($array);
output:
array(2) {
[0]=> array(2) {
[0]=> string(3) "IPT"
[1]=> string(8) "notmatch"
}
[1]=> &array(2) {
[0]=> string(7) "neither"
[1]=> string(3) "IPT"
}
}
Note that you have to loop through each row by reference (&$row) otherwise the original array will not be modified.
Your value does not sit in the array as a simple element but as a subset right? Like so?
array (
array ('id' => 45, 'name' => 'peter', 'whatyouarelookingfor' => '5F'),
array ('id' => 87, 'name' => 'susan', 'whatyouarelookingfor' => '8C'),
array ('id' => 92, 'name' => 'frank', 'whatyouarelookingfor' => '9R')
)
if so:
<?php
foreach ($array as $key => $value) {
$array[$key]['whatyouarelookingfor'] =
preg_replace("/\d?\dIPT\.\w/", "IPT", $value['whatyouarelookingfor']);
}
Or if more elements have this, just go broader:
<?php
foreach ($array as $key => $value) {
$array[$key] =
preg_replace("/\d?\dIPT\.\w/", "IPT", $value);
}
your $array contains some further arrays. preg_replace works fine with arrays of strings, but not with arrays of arrays [of arrays...] of strings.
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][*]"
}
}