detecting phone number format in PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have about 10 thousand strings that I want to extract phone numbers from, some of the phone number format is the following:
08978803929
085282486601
081284671191
+6285722345678
The pattern is as follows, it's either a consecutive string of numbers starting with 08, it can either be 11 digit or 12 digit. It can also have an area code of +62 in front of it. How do I extract all of these string using a regular expression in PHP?
To make it even more simpler, I want to detect strings that starts with 08 or +62 or just 62

You can use this regex:
^(\+?62|08)[0-9]{9,11}$
Explanation:
- ^ and $ - Line start and Line end
- (\+?62|08) - starting with 62 OR +61 OR 08
[0-9]{9,11} - match 9 to 11 digits

Related

RegEx to find a pattern [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Looking for regex to do the following in php:
Identify if the given string is in the pattern string1_string2_number e.g ph_val_34563, ph_val_296987 etc.
When true, extract the number part.
regex = "^[A-Za-z]+_[A-Za-z]+_(\d+)$"
Assuming that the valid characters for your strings are letters, [A-Za-z]+ says to expect a group of one or more letters. The _ following these character classes says that an _ must follow.
(\d+) says to group, and capture, a set of one or more numbers following the previous expression.
^ says: "begins with"
$ says: "ends with"
You should take a look at a tutorial on regular expressions.

comparision not working for file_get_content value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a file with content
1
2
3
<?php
$input=file_get_contents('/home/abhishek/Desktop/input');
echo $input ;// i get 1 2 3 as output
$x='1 2 3';
if(trim($input)==trim($x))
echo "hellllliff--";
else
echo "helllllelse";
?>
why it goes to else????
The new line characters need to be removed - trim would only trim whitespace, not the \n \r characters.
preg_replace("/[\n\r]/","",$input);
Would get rid of them if that's the intention. Or alter your comparison string to have new lines too (although watch out for the unix versus pc variance in the new line character)
1
2
3 in three lines is not equals to
1 2 3 in one line
so it's get false, when compare.For further explaination,$input = 1/n2/n3

How to use PHP rexep or preg_match to extract letters and numbers from a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Working with a ficticuous string such as;
$string = 'Ford : LTD 1988 Ford Station Wagon with HP 351 H Engine and Performance Transmission';
How could I use rexexp or preg_match (I don't know which would be better either) to extract a sequence of letters and numbers ("HP 351 H") from that string to use in another variable (ie: $EngineSize)
The above is a fictious example, I'm just trying to make it clear that I'm trying to extract letters and numbers from a UI.
NOTE: Being that this is coming from a UI, the engine size may be positioned anywhere in the string and the format may be with or without spaces and may or may not have a letter at the end, as well as the model could be 2 or 3 letters (ie; LE, LT, LTD etc) as well as the engine size could be 2 - 3 digits possibly followed by 2 or three letters).
If anyone wouldn't mind showing me how to write an expression to retrieve this data and explain to me which is better (regexp or preg_match) I'd be most appreciative and I thank you in advance.
The following regex matches exactly what you describe, but there is a good chance of false positives:
/(?<=\s|^)[a-zA-Z]{2,3} ?\d\d\d? ?[a-zA-Z]{1,3}?(?=\s|$)/

Php regex for number with point after each three digits [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to make a number validation that checks if the user hasn't already added the points after each three digits. I plan to do this verification using refex
So for example 11.231.121.313 is a valid number, also 11231121313 but 11231.121.313 is not.
^(\d+|\d{1,3}(\.\d{1,3})*)$
The first alternation allows you to have simply all digits. The second checks for 1-3 digits optionally followed by groups of a decimal point with 1-3 following digits. This works for your examples.
try this
if (preg_match('/^(\d{1,3}(\.\d{3})+|\d+)$/', $number)) {
// correct number
}
UPD: Add expression for only numbers

Build my specific REgular Expression [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to build a regular expression for a specific problem but I cannot achieve to do it. I have a structure of a string like this 01422756860-8.
As you can see there is a string of length 13. The first 11 characters should be numbers in the interval of [0-9], the next character is a line(minus) and the last character is again a number. There shouldn be any space in between these characters.
Could anyone please help me to do this? I am going to use this regex in php.
I look forward on your answer.
Thank You.
What's the problem?
/^[0-9]{11}-[0-9]$/
Demo
Autopsy:
^ The string must start here
[0-9]{11} Any digit from 0 to 9 repeated exactly 11 times
- A literal dash
[0-9] Any single digit from 0 to 9
$ The string must end here
This regular expression should do it
\d{11}-\d

Categories