preg_match and regex error while parsing string? - php

I need to scrap a part of a url using preg_match but I never got what I need.
Here the example:
$item = "http://example.com/0229883504/?r=2-OR1&p=1";
$item = preg_match_all("/href[^\"]+/i",$item,$matches);
print_r($matches)
I need to return this number
0229883504
I tried a lot but when I var_dump the matches array, it gives:
Array ( [0] => Array ( [0] => href= ) )
I know that the problem is within the pattern but I'm not so good in this part :)

This is the code you need:
$item = "http://example.com/0229883504/?r=2-OR1&p=1";
$item = preg_match_all("#http://.*?/(.*?)/.*#i",$item,$matches);
print_r($matches);
If you need to extract the value 0229883504, you can add these lines:
$result = $matches[1][0];
echo $result;
and it will work as you can see here: http://ideone.com/H2E9I

This will do the trick for example above.
preg_match_all("/http:\/\/example.com\/([a-zA-Z0-9]+)\//",$item,$matches)
or even better:
preg_match_all("/http:\/\/example.com\/(.+)\//",$item,$matches)
However if your domains can vary use the example of the code from Aurelio :).

Related

Extract all urls from preg_match_all

I'm working on my code to fetch the href urls from the variable $message after when I'm fetching the data from the database. I have got a problem with using preg_match_all to fetch the href tags from the variable because it will display the array in the output like twice.
Here is the output:
Array ( [0] => Array ( [0] => https://example.com/s-6?sub=myuserid [1] => https://example.com/s-6?sub=myuserid
[2] => https://example.com/s-6?sub=myuserid [3] => https://www.example2.com/1340253724 [4] => https://example.com/s-6?sub=myuserid ) )
It should be:
Array ( [0] => https://example.com/s-6?sub=myuserid [1] => https://example.com/s-6?sub=myuserid
[2] => https://example.com/s-6?sub=myuserid [3] => https://www.example2.com/1340253724 [4] => https://example.com/s-6?sub=myuserid ) )
Here is a minimal example:
<?php
$message = 'Click Here!
Watch The Video Here!
HERE
Example2.com/1340253724
Here';
//find the href urls from the variable
$regex = '/https?\:\/\/[^\" ]+/i';
preg_match_all($regex, $message, $matches);
print_r(matches);
?>
I have tried to use a different way like this:
foreach($matches as $url)
{
echo $url;
}
And also I have tried this:
foreach($matches as $url)
{
$urls_array[] = $url;
}
print_r($urls_array);
The results are still the same. I have tried to find the answer on google, but I can't find the answer for a solution.
Unfortunately, I am not be able to find the solution for this, because I have got no idea how I can fetch the href tags using preg_match_all to display the elements and store in the array.
The problem I have found that something have to do with the variable called $matches.
Can you please show me an example how I can use to fetch the href tags using with preg_match_all so I could store be able to store the elements in the array?
Thank you.
As wrote in documentation preg_match_all
$out[0] contains array of strings that matched full pattern, and
$out[ 1] contains array of strings enclosed by tags.
So you could do like following
foreach($matches[0] as $url)
{
echo $url;
}
Try this:
foreach($matches[0] as $url)
{
echo $url;
}
Hi,
as far as I correct understand your problem is that u received one to much nested array with results and you cant read yours URL that are also as array?
One of the solution that u can use is getting rid of unnecessary nested array. You can do this by using PHP Array function array_shift().
From php.net manual
array_shift() shifts the first value of the array off and returns it [...]
So the trick is that returned value will be your array with data through which you can loop.
A bit of sample with your case:
//from the moment when you use preg_match_all and have matches
preg_match_all($regex, $message, $matches);
$urls = array_shift($matches);
foreach($urls as $url) {
//do something with URL
}
Of course you can different use array_shift(), thats just a simple sample ;)
Cheers!

How to get a part of url from php

how can I use php to get exactly the id from google play url.
Example:
Google Play Url: https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en
I want to get the com.zing.zalo . Thank you!
Simple Way
use preg_match or get the id from the url using $_GET['id'], if
you get this from url as other answer did.
$Url = "https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en";
preg_match("/[^?]+(?:\?id=([^&]+).*)?/", "$Url", $matches);
echo $matches[1]; //com.zing.zalo
Working Example here Check online
The Longest way:
Simply you can use some PHP function to get it. Lets you have the following url.
$Url = "https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en";
so what you need to explode the url using ? which is only one on the url.
$arr = explode("?", $Url);
From that array you need to store only the second part cause you need query string. So take only $arr[1]. Now explode again the $arr[1] with the & sign which is divide the rest of the url i mean $arr[1].
$arr2 = explode("&", $arr[1]);
Now you are all set, use another explode function to get the com.zing.zalo from the $arr2[0].
$idval = explode("=", $arr2[0]);
Result, Just echo the second part of the $idval array.
echo $idval[1]; //com.zing.zalo
Use $_GET['id'] to get the query string value of id
<?php
echo $_GET['id'];
?>
You could do this with regex: (?:\?id=)(.*)\b (I'm sure there's a more effective regex for this, but this accomplishes what you require)
preg_match('/(?:\?id=)(.*)\b/', 'https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en', $matches);
print_r($matches);
Returns:
Array
(
[0] => ?id=com.zing.zalo&
[1] => com.zing.zalo
)

To look for a simple way to extract matched parts of strings from an array

I want to extract matched parts of strings --digital part from an array
array("HK00003.Day","HK00005.Day").
<?php
$arr=array("HK00003.Day","HK00005.Day");
$result= array();
foreach ($arr as $item){
preg_match('/[0-9]+/',$item,$match);
array_push($result,$match[0]);
}
It can get the result :00003 00005,it seems tedious,preg_grep seems simple but the result is not what i want .
preg_grep('/[0-9]+/',$arr);
The output is "HK00003.Day","HK00005.Day", not 00003 00005,
is there more simple way to get the job done?
You can use preg_filter (which already uses preg_replace and does not require additional callback functions) to replace the each entry in the array with the number inside:
<?php
$arr = array("HK00003.Day","HK00005.Day");
$matches = preg_filter('/^.*?([0-9]+).*/', '$1',$arr);
print_r($matches);
?>
Output of a sample program:
Array
(
[0] => 00003
[1] => 00005
)
This should work for you:
(Here I just get rid off every character in your array which isn't a number with preg_replace())
<?php
$arr = ["HK00003.Day", "HK00005.Day"];
$result = preg_replace("/[^0-9]/", "", $arr);
print_r($result);
?>
output:
Array ( [0] => 00003 [1] => 00005 )
Your code is fine, not tedious at all. If you want a one-liner you can try something like this (remove everything that's not a digit):
array_push($result, preg_replace("~[^0-9]~", "", $item));
preg_grep return array entries that match the pattern! Therefore, it returns an array of entry rather than the matching string
try below:
preg_match_all('/[0-9]+/',implode('-',$arr),$result);

How to pick numbers between underlines using regex?

I want to get only the value in bold, but I'm not getting.
349141_194419414_4828414_n.jpg
or
https:// hphotos-ash3.net/t1.0-9/1146_54482593153_1214114_n.jpg
Thank you already
You can use preg_match with a capture group to get the result:
<?php
$searchText = "349141_194419414_4828414_n.jpg";
$result = preg_match("/_(\\d+)_/u", $searchText, $matches);
print_r($matches[1]);
?>
output:
194419414
(i'm not sure whether this one is good method or not but you can get whatever value you want to by this)
$r="349141_194419414_4828414_n";
print_r(explode('_',$r));
output:
Array ( [0] => 349141 [1] => 194419414 [2] => 4828414 [3] => n )
$rr=explode('_',$r);
echo $rr[1];
output
194419414
Try something like this:
.+/\d+_(\d+)_\d+_n.jpg
Here's a regular expression answer.
$filename = '349141_194419414_4828414_n.jpg';
preg_match_all('/[0-9]+/', $filename, $matches);
echo $matches[0][1]; //194419414

How to extract parts using regular expression in PHP?

For example you have the following string:
$text = "word1:text1#atpart/foo/do/myfood$textfinal";
The function will work like:
$parts = array();
extract( $regular_exp, $text, $parts );
In the parts array we will get this:
$parts[0] = "word1";
$parts[1] = "text1";
$parts[2] = "atpart";
$parts[3] = "/foo/do/myfood";
$parts[4] = "textfinal";
Thanks!
This may not be what you are after, but the format you show looks almost like a URL with a username:password#domain authentication in front. If you can get the last $ to be served as a ?, it might be an idea to use parse_url() to parse it.
$string = "word1:text1#atpart/foo/do/myfood?textfinal"; // notice the ?
$string = "none://".$string; // We need to add a protocol for this to work
print_r (parse_url($string));
Result:
Array (
[scheme] => none
[host] => atpart
[user] => word1
[pass] => text1
[path] => /foo/do/myfood
[query] => textfinal )
the advantage of this would be that it's pretty flexible if one or more parts can be missing in the incoming data. If that's not an issue, a regex may be more convenient.
try
$parts = preg_split('/[:#\$]+/', $text);
Without more details, this matches the proposed example:
preg_match('#(.*?):(.*?)#(.*?)(/.*?)\$(.*)#', $text, $parts);
note that you will get the parts starting at index 1 instead of 0.
$delims=':#$';
$word = strtok('word1:text1#atpart/foo/do/myfood$textfinal',$delims);
while ( $word!==false ) {
foreach( explode('/',$word,2) as $tmp){
$words[]=$tmp;
}
$word = strtok($delims);
}
var_dump($words);
On one hand this is probably overkill. On the other hand, this may be more flexible depending on how different the string can be.
Demo: http://codepad.org/vy5b9yX7
Docs: http://php.net/strtok

Categories