I want to search a phone number from a whole sentence. It can be any number with a pattern like (122) 221-2172 or 122-221-2172 or (122)-221-2172 by help of PHP where I don't know in which part of the sentence that number is exists or I could use substr.
Search a Number pattern
828 Views Asked by Soumya At
2
There are 2 best solutions below
0
ritwikVerma
On
$text = 'foofoo 122-221-2172 barbar 122 2212172 foofoo ';
$text .= ' 122 221 2172 barbar 1222212172 foofoo 122-221-2172';
$matches = array();
// returns all results in array $matches
preg_match_all('/[0-9]{3}[\-][0-9]{6}|[0-9]{3}[\s][0-9]{6}|[0-9]{3}[\s][0-9]{3}[\s][0-9]{4}|[0-9]{9}|[0-9]{3}[\-][0-9]{3}[\-][0-9]{4}/', $text, $matches);
$matches = $matches[0];
var_dump($matches);
Related Questions in PHP
- How to add the dynamic new rows from my registration form in my database?
- Issue in payment form gateway
- How to create a facet for WP gridbuilder that displays both parent and child custom fields?
- Function in anonymous Laravel Blade component
- How to change woocomerce or full wordpress currency with value from USD to AUD
- General questions about creating a custom theme Moodle CMS
- How to add logging to an abstract class in php
- error 500 on IIS FastCGI but no clue despite multiple error loggings activated
- Composer installation fails and reverts ./composer.json and ./composer.lock to original content
- How to isolate PHP apps from each other on a local machine(Windows or Linux)?
- Laravel: Using belongsToMany relationship with MongoDB
- window.location.href redirects but is causing problems on the webpage
- Key provided is shorter than 256 bits, only 64 bits provided
- Laravel's whereBetween method not working with two timestamps
- Implementing UUID as primary key in Laravel intermediate table
Related Questions in STRING
- What does: "char *argv[]" mean?
- User input sanitization program, which takes a specific amount of arguments and passes the execution to a bash script
- JSON Body is Not Passing Certain Strings
- Regex to match repeated substring in Google Sheets
- Find the sum of the numbers in the sequence
- Hello, how can I use a block parameter of withstyle parameter when we create a annotated string in jetpackpack compose
- How to convert an HTML string to an escaped one?
- Quintic Number Number Counting Hash Function
- From Buffer("string", "hex) to string JS
- Calling ToString with a nominated format returns Char rather than String
- How to update an already existing array by accessing it by a variable with the exact same name assigned to it
- Why does \b not interpreted as backslash in this regular expression
- Python: why aren’t strings being internalized if they are received from ints by using str()?
- If the element(s) in the first list equal element(s) of the second list, replace with element(s) of the third list
- About Suffix Trees features
Related Questions in PHONE-NUMBER
- Why am I failing to get the new phone-picker to work properly?
- Any flaws in my International Phone Number regex?
- Understanding Digit Repetition in Phone Numbers
- Fetch Billing phone number Wordpress
- formatting phone numbers to a single view
- How can JavaScript format (Phone Numbers) two elements on the same page?
- Phone number formatting with a dot after the country code
- Does Amazon SNS support publishing SMSes in batch to different mobile numbers?
- how to get telegram id using phone number
- Mismatch between A2P Compliance CSV and Regulatory / Messaging Service APIs
- HTML5 input pattern for a 10 digit US phone number
- Phone Number Validations
- Designing a Secure and Privacy-preserving Phone Number Change System in a SMS SaaS Product
- aws cognito phone number regex validation faild
- Why are Indian phone numbers automatically prepending a 0 in Maz-UI
Related Questions in TEXT-SEARCH
- Google New Text Search (API) doesn't return expected response
- How do I use a Mongodb aggregation variable such as the ones defined using `let` in $text operator search?
- Mongo Atlas combine $search with match
- Goole Maps API - Text Search New - LocationRestriction doesn't work
- Isar: Is there a way to do a text search on the items of an IsarLinks object using indexes?
- How to do full field match in Solr?
- Categorize Bank Transactions in Excel based on Keywords from another table
- Text search in Solr where the field is the substring of the query
- How to use Puppeteer to count the occurrences of a specific text on a web page?
- Excel - FIND function (case-sensitive) ignores case?
- Databases: Effectively implement string contains query
- How to sort and search in text while ignoring diacritics of all kinds?
- Text match and search on Mysql table column
- Partial text search in mongoDB
- Substring search with a (regex?) condition Python
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
You can use regular expressions to solve this. Not 100% on php syntax, but I imagine it would look something like:
^says "begins with"\(escapes the(\(?say 0 or 1(\d{x}says exactlyxnumbersYou may also want to check out Using Regular Expressions with PHP