PHP preg_match_all return empty - php

This is my code:
$str = "sdf13631389945fssx6221363138994523213af";
preg_match_all("/^1\d{10}$/",$str,$result);
var_dump($result);
I want to match the phone no. 13631389945 but don't want to match this phone no. in 6221363138994523213 , so I write this, but it return empty, can you help me write a right pattern, thanks a lot!

It doesn't return a result because the whole string doesn't match the expression. If you only want part of the string to match then get rid of the ^ and $.
preg_match_all("/1\d{10}/",$str,$result);
To not match the phone number in the second part of the string you are going to have to explain what exactly makes it an invalid match. If it's the surrounding numbers then use this:
(?:^|[^\d])(1\d{10})(?:$|[^\d])

Related

Regex for a string used for save a telephone number

I have a question. How should look a regex for the following words:
+23456745678 or +29845281058, (with comma)
I tried but no result:
if (!preg_match('#^\+[0-9]{11}$#',$string)) {
return ("Msisdn $string is incorrect!");
break;
}
Always I get this error.
Please help me. Thx in advance
The pattern should be:
'/\+[0-9]{11},?/'
The ^ in the beginning and $ at the end stand for beginning and end of line, which I am not sure if you really want there (my guess is not).
Test it here: http://www.phpliveregex.com/p/78v
Another option is to use \d for digit
'/\+\d{11},?/'

Match only the first set of numbers in a string

I need to retrieve the first set of numbers from a string, but I'm not sure how.
I have the following, which I was expecting to pick each set of numbers so that I could then pick the first key from the $matches array, but it literally matches only the first number.
In this example I'd be looking for '123'. Can someone please let me know how to do this with RegEx (or a better way if RegEx is not best for the job). Thanks.
$e = 'abc 123,456,def, 789-ab-552'; // Just a random example
$pattern = "/[0-9]/";
preg_match($pattern, $e, $matches);
You must add a quantifier:
$pattern = "/[0-9]+/";
+ means one or more
You can find an ajax regex tester for php here and more informations about regular expressions here.

Regex to get string from last numeric values

I have some php string like below
abc-1987-mp3-songs
xyz-1999-india-mp3-songs
dec-2001-mp3-songs
ryu-2012-freemp3-songs
Now I want these string splited at last found numeric values like below
abc-1987
xyz-1999
dec-2001
ryu-2012
Please help me that which regex can be used to do this. thanks.
Ok, I had a look (do take some time to learn regex - but meanwhile):
$split = (preg_split('/(^.*?[0-9]+)\-?[^0-9]+/', 'foo-xyz-1999-india-mp3-songs', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
echo $split[0];//<--- foo-xyz-1999, just like you wanted
Dumps an array with foo-xyz-1999 as first value, which is what you need. If you want to know what every part of the regex does read it here
The only difference is that, though the whole string becomes its own delimiter, there are two delimiters (the first part, always ending on a series of numbers and the rest of the string, that doesn't contain any more digits)
Use explode insted of regular expression
for example:-
$str="abc-1987-mp3-songs";
$f=explode("-",$str);
echo $final_result=$f[0]."-".$f[1];
or if you want to use reg exp.then
<?php
$str="abc-1987-mp3-songs";
echo $f=preg_replace('/[^0-9]/','', $str);
?>
Above code give you all the numeric digits of your string.
This would match last occurrence of numeric value from given string:
([\w\d-]*-[\d]+)
This is the link: Regex

php regex matching first and last

i'm new to regular expressions and would like to match the first and last occurrences of a term in php. for instance in this line:
"charlie, mary,bob,bob,mary, charlie, charlie, mary,bob,bob,mary,charlie"
i would like to just access the first and last "charlie", but not the two in the middle. how would i just match on the first and last occurrence of a term?
thanks
If you know what substring you're looking for (ie. it's not a regex pattern), and you're just looking for the positions of your substrings, you could just simply use these:
strpos — Find position of first occurrence of a string
strrpos — Find position of last occurrence of a char in a string
Try this regular expression:
^(\w+),.*\1
The greedy * quantifier will take care that the string between the first word (\w+) and another occurrence of that word (\1, match of the first grouping) is as large as possible.
You need to add ^ and $ symbols to your regular expression.
^ - matches start of the string
$ - matches end of the string
In your case it will be ^charlie to match first sample and charlie$ to match last sample. Or if you want to match both then it will be ^charlie|charlie$.
See also Start of String and End of String Anchors for more details about these symbols.
Try exploding the string.
$names = "charlie, mary,bob,bob,mary, charlie, charlie, mary,bob,bob,mary,charlie";
$names_array = explode(",", $names);
After doing this, you've got an array with the names. You want the last, so it will be at position 0.
$first = $names_array[0];
It gets a little trickier with the last. You have to know how many names you have [count()] and then, since the array starts counting from 0, you'll have to substract one.
$last = $names_array[count($names_array)-1];
I know it may not be the best answer possible, nor the most effective, but I think it's how you really start getting programming, by solving smaller problems.
Good luck.

Problems with PHP PCRE

I'm having a problem with PHP PCRE, and I'm used to POSIX, so I'm not too sure about what I'm doing wrong. Basically, this function is matching up to 10 numbers separated by commas. However, it's also matching the string sdf (and probably many others), which I don't see the reason for. Can anyone help me?
$pattern='^\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?^';
$leftcheck=preg_match($pattern, $leftmodules);
$centercheck=preg_match($pattern, $centermodules);
$rightcheck=preg_match($pattern, $rightmodules);
if(!$leftcheck OR !$centercheck OR !$rightcheck)
{
$editpage = $_SERVER['HTTP_REFERER'].'?&error=1';
die("Location:$editpage");
}
^\d{1,5}(, *\d{1,5}){0,9}$
I'm assuming the following:
Spaces may or may not be there.
Numbers can be any length.
Only numbers, spaces, and comma's are allowed.
Trailing commas without a number after them are allowed.
Between 1 and 10 numbers seperated by commas are ok.
Given that:
$pattern = '/^(\d+,* *){1,10}$/';
works.
From what I can see, the regular expression you provided will match anything you pass into it. Here's why
\d{0,5} #\d matches any digit character, while {0,5} means the
#preceding character must be repeated between **0** and five times
So your regular expression is essentially short circuiting. The engine see the first character of your string and says "has a digit been repeated 0 times? Yes? OK, it's a match!
I think if your number are only separated by commas something like this should do it
$pattern = '^\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5}$';
You need to contain the pattern between two equal symbols for it to be valid. People usually use /.
$pattern = '/some pattern/';
To match the whole thing you want to have ^ at the start and $ at the end. Getting this wrong is probably why your sdf was matching.
$pattern = '/^whole pattern match$/';
It's a bit confusing how the numbers will be separated. Is it comma or space? Is both OK? What about none? Here's my best guess though.
$pattern = '/^\d{,5}[, ](\d{,5}[, ]){,9}$/';

Categories