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
Related
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!
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);
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 :).
Hi all
how can i get number(positive num) from string,if string syntax is the following:
t_def_type_id_2 t_def_type_id_22 t_def_type_id_334
so,in the first string i want to get 1,and in the second i want to get 22 and in the third string i want to get 334 using preg_match_all or any other sutable php function
You can use the regex
\d+$
with preg_match
if there is only one number in the string, simply use \d+
Try this:
preg_match('/^\w+(\d+)$/U', $string, $match);
$value = (int) $match[1];
You can use
str_replace('t_def_type_id_','');
what about following code:
^[\d]+(\d+)$
You can use preg_replace():
$defTypeID = preg_replace("/^(.*?)(\d+)$/", "$2", $defTypeIDString);
$string = "t_def_type_id_2
t_def_type_id_22
t_def_type_id_334";
preg_match_all("#t_def_type_id_([0-9]+)#is", $string, $matches);
$matches = $matches[1];
print_r($matches);
Result:
Array
(
[0] => 2
[1] => 22
[2] => 334
)
If it's always the last thing in your string, then using a banal string function approach is possible and looks a bit compacter:
$num = ltrim(strrchr($string, "_"), "_");
You may use
^\w+(\d+)$
but I did not test
Here's my alternative solution.
$number = array_pop(explode('_', $string));
I'm trying to get the post-id my posts.
this is the format it is in--> postid-785645
the numbers are randomly generated.
<?php
$mysite = "http://example.com";
$wholepage = #file_get_contents($mysite);
// I want to use preg_match to echo just the numbers. out of the whole page.
// Basically i'm trying to have is search the whole page for -> postid-(randomly generated number) and have it echo the number from things that are in this format.
?>
You could try using the following regular expression:
"/postid-[0-9]+/"
Example code:
$wholepage = 'foo postid-785645 bar postid-785646 baz';
$matches = array();
$string = preg_match_all("/postid-[0-9]+/", $wholepage, $matches);
print_r($matches);
Result:
Array
(
[0] => Array
(
[0] => postid-785645
[1] => postid-785646
)
)
I'm using preg_match_all instead of preg_match because preg_match stops after it finds the first match.