preg_match_all doesn't reply matches array? - php

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) ""
}
}

Related

Replace data in array based on regex

I have the next array with data (which is dynamically generated).
Now I want to do some Magic and tweak the array.
array(1) {
["table"]=>
array(3) {
["header"]=>
array(4) {
[0]=>
array(1) {
["c"]=>
string(4) "Naam"
}
[1]=>
array(1) {
["c"]=>
string(7) "Functie"
}
[2]=>
array(1) {
["c"]=>
string(13) "Nevenfuncties"
}
[3]=>
array(1) {
["c"]=>
string(34) " commissies"
}
}
["caption"]=>
bool(false)
["body"]=>
array(3) {
[0]=>
array(4) {
[0]=>
array(1) {
["c"]=>
string(16) "*|class:orange|*"
}
[1]=>
array(1) {
["c"]=>
string(6) "dsasad"
}
[2]=>
array(1) {
["c"]=>
string(0) ""
}
[3]=>
array(1) {
["c"]=>
string(0) ""
}
}
[1]=>
array(4) {
[0]=>
array(1) {
["c"]=>
string(4) "brrr"
}
[1]=>
array(1) {
["c"]=>
string(6) "adsdsa"
}
[2]=>
array(1) {
["c"]=>
string(0) ""
}
[3]=>
array(1) {
["c"]=>
string(0) ""
}
}
[2]=>
array(4) {
[0]=>
array(1) {
["c"]=>
string(6) "dsasad"
}
[1]=>
array(1) {
["c"]=>
string(6) "dsadas"
}
[2]=>
array(1) {
["c"]=>
string(4) "dsad"
}
[3]=>
array(1) {
["c"]=>
string(0) ""
}
}
}
}
}
When we look at the ['header'] it contains ['c'] (the cell data). This can be text, but also a tag.
For example: *|class:orange|* here some text.
Now I want to split those up and overwrite the ['c'] if it contains '|class:orange|'.
So when you have this:
array(1) {
["c"]=>
string(7) "*|class:orange|* hello"
}
It would become this:
array(2) {
["c"]=>
string(7) "hello",
["class"]=>
string(7) "orange",
}
This way I could split the class and add it to the array. But I am stuck at the preg_match.
foreach ($table as &$row) {
foreach ($row['header'] as &$header) {
// $header['class'] = 123;
preg_match('/\*\|class:([^\|])\|\*/', $header['c'], $matches);
}
}
I need to do 2 things
Add an attribute to the array ($header['class']) with the class after class:example.
I need to replace the $header['c'] so it does not contain *|class:orange|* and only the rest of the text.
No need for regexp (yet). Tags can be found with delimiter positioning:
foreach ($row['header'] as &$header) {
$str = $header['c'];
$tagged = substr($str, 0, 2) === '*|' && $pos = strpos($str, '|* ');
if (!$tagged) { continue; }
[$tag, $value] = explode(':', substr($str, 2, $pos - 2));
$header['c'] = substr($str, $pos + 3);
$header[$tag] = $value;
}
You could for example use 2 capturing groups and use those as the values for the c and new class key.
For the second capturing group you could make sure to match at least a single non whitespace char using \S
Note to repeat the character class 1 or more times and you don't have to escape the pipe in the character class.
\*\|class:([^|]+)\|\*\h*(\S.*)
Or if what follows for group 2 can be optional:
\*\|class:([^\|]+)\|\*\h*(.*)
Regex demo
Explanation first pattern
\*\| Match *|
class: Match literally
([^|]+) Capture group 1, match 1+ times any char except |
\|\*\h* Match |* followed by 0+ horizontal whitespace chars
(\S.*) Capture group 2, match a non whitespace char and 0+ times any char except a newline
Regex demo | Php demo
Example code
$array = [
"c" => "*|class:orange|* hello"
];
$pattern = "~\*\|class:([^|]+)\|\*\h*(\S.*)~";
foreach ($array as $key => $string) {
if (preg_match($pattern, $string, $matches)) {
$array[$key] = $matches[2];
$array["class"] = $matches[1];
}
}
print_r($array);
Output
Array
(
[c] => hello
[class] => orange
)

How to get the keys from preg_match_all?

The code was working:
$url = 'http://www.google.com/search?hl=en&tbo=d&site=&source=hp&q=upoznavanje';
$html = file_get_html($url);
preg_match_all('/(?<="><cite>).*?(?=<\/cite><div\ class=)/', $html, $output);
foreach ($output[0] as $link) {
$link ."<br>" ;
}
When I added echo $output[0], now I get 0, and nothing in $output[1].
var_dump works and print_r array is there, but how do I get unique values of each without foreach?
var_dump:
array(1) { [0]=> array(10) { [0]=> string(21) "https://www.elmaz.rs/" [1]=> string(47) "https://badoo.com/sr/upoznavanje/serbia/" [2]=> string(39) "https://serbiandating.com/?locale=sr_RS" [3]=> string(30) "https://www.lepoticaizver.com/" [4]=> string(22) "www.prvi-sastanak.net/" [5]=> string(122) "https://www.telegraf.rs/.../1878507-top-5-aplikacija-za-muvanje-ovo-su- najbolji-sajtovi-za-upoznavanje-i-dejt-foto" [6]=> string(137) "https://www.telegraf.rs/.../1863749-dopisivala-sam-se-sa-muskarcinama-na- sajtu-za-upoznavanje-uh-kakve-sam-sve-ponude-dobila-foto" [7]=> string(24) "https://www.iskrica.com/"
I found how to do that with extracting single data from array without loop:
print_r($output[0][0]);

Spliting Single Column Query values to different php variables

need some help with splitting mysql single column query array into different php variables here.
example:
here's the query, it's pretty simple to be honest.
but, i'm running out of ideas right now.
$string = "select Description from tblQuestion
where Employeeid = '$param'"
$query = $this->db->query($string);
$result = return $query->result_array();
btw, i am using Codeigniter and i tried to var_dump and the results are like this.
array(9) { [0]=> array(1) { ["Description"]=> string(5) "tidak" } [1]=> array(1) { ["Description"]=> string(5) "tidak" } [2]=> array(1) { ["Description"]=> string(5) "tidak" } [3]=> array(1) { ["Description"]=> string(5) "tidak" } [4]=> array(1) { ["Description"]=> string(5) "tidak" } [5]=> array(1) { ["Description"]=> string(5) "tidak" } [6]=> array(1) { ["Description"]=> string(5) "tidak" } [7]=> array(1) { ["Description"]=> string(5) "tidak" } [8]=> array(1) { ["Description"]=> string(5) "tidak" } }
i tried to use json_encode and the result is
[{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"}]
the question is.
how do i convert this stack of arrays into different variables like this?
$var0 = "tidak";
$var1 = "tidak";
$var2 = "tidak";
$var3 = "tidak";
and on and on....
thanks in advance.
cheers!
Put the results in a foreach loop and assign the values to a dynamic variable...
sample code like,
foreach($results as $key=>$val){
$str = 'var'.$key;
$$str = $val['Description'];
}
echo $var0;

Array instead of Array Element

I'm trying to turn Minecraft names into UUIDs, which can be converted into a "name history". I have the name->UUID part down pat, but the name history is getting an error.
Running this code, the $uuid variable contains an array of all the UUIDs I'm trying to convert into a name history - I then try to run all of them through the API, and depending on how many past names the user has, add » icons to signify a change. Unfortunately, with test data of something like this:
Foo
Bar (changed to Baz)
it outputs
Array | Array » Array |
, not the
Foo | Bar » Baz |
it's supposed to. I know I'm probably getting the array as an object, but I don't know where I would be doing that or how to get the element instead.
(and here's the code)
$uuid_real = json_decode($uuid, TRUE);
foreach($uuid_real as $uuid_totest){
$toadd = json_decode(file_get_contents('https://api.mojang.com/user/profiles/' . $uuid_totest['id'] . '/names'), TRUE);
if(count($toadd)==1){
$results .= $toadd['0'] . " | ";
}elseif(count($toadd)==2){
$results .= $toadd['0'] . " » " . $toadd['1'] . " | ";
}elseif(count($toadd)==3){
$results .= $toadd['0'] . " » " . $toadd['1'] . " » " . $toadd['2'] . " | ";
}
Here is var_dump for $toadd with some currently online users:
array(1) {
[0]=>
array(1) {
["name"]=>
string(10) "lottie1664"
}
}
array(1) {
[0]=>
array(1) {
["name"]=>
string(12) "wingmanfoutz"
}
}
array(1) {
[0]=>
array(1) {
["name"]=>
string(11) "bigfoot2991"
}
}
array(1) {
[0]=>
array(1) {
["name"]=>
string(13) "mrstampycat05"
}
}
array(1) {
[0]=>
array(1) {
["name"]=>
string(10) "MEHLAWLARZ"
}
}
array(1) {
[0]=>
array(1) {
["name"]=>
string(10) "metboy2002"
}
}
array(1) {
[0]=>
array(1) {
["name"]=>
string(10) "SILVERMAN2"
}
}
array(2) {
[0]=>
array(1) {
["name"]=>
string(11) "salesman200"
}
[1]=>
array(2) {
["name"]=>
string(8) "Quartzic"
["changedToAt"]=>
int(1423055736000)
}
}
array(1) {
[0]=>
array(1) {
["name"]=>
string(13) "MercenaryCrow"
}
}
array(1) {
[0]=>
array(1) {
["name"]=>
string(8) "fishmeal"
}
}
It is because $toadd[0] $toadd[1] and $toadd[2] are arrays not strings, please provide here var_dump($toadd) and we can help you
EDIT:
So as you can see there is no string under $toadd[x] but an array looking like this :
array(1) {
["name"]=>
string(10) "lottie1664"
}
so modify your code and use :
$toadd[0]['name']
$toadd[1]['name']
$toadd[2]['name']

How to create mentions for names like "#myname" using javascript?

I'm planning to build web app and i heve question how to create mentions for name "#myname" like facebook or twitter
Find them in a string with this regex....
$str = 'Yo #bob, what\'s up? I have a new email, tom#bob.com, tell #john too, from #alex';
preg_match_all('/\s#(?P<mention>\w+?)\b/', $str, $mentions);
var_dump($mentions);
Output
array(3) {
[0]=>
array(3) {
[0]=>
string(5) " #bob"
[1]=>
string(6) " #john"
[2]=>
string(6) " #alex"
}
["mention"]=>
array(3) {
[0]=>
string(3) "bob"
[1]=>
string(4) "john"
[2]=>
string(4) "alex"
}
[1]=>
array(3) {
[0]=>
string(3) "bob"
[1]=>
string(4) "john"
[2]=>
string(4) "alex"
}
}
Of course, you could real time detect them in a string in JavaScript, just change that regex to a JavaScript one.
Then, you would look up your database based on the tagged name, and then do what you need to do!
You could cut down on requests by limiting your regex to what makes a valid username, e.g. /\w{6,}/.
function mention($txt)
{
$txt = ' '.$txt;
preg_match_all('/\s#(?P<mention>\w+?)\b/', $txt, $mentions);
echo "<pre>";
print_r($mentions);
if (isset($mentions[0])) {
foreach ($mentions[0] as $key => $value) {
$mention = strtolower(str_replace(array("#"," "), "", $value));
$txt = str_replace($value, ' '.$mention.'', $txt);
}
}
return trim($txt);
}

Categories