arrays - PHP preg_match a specific pattern in hostname -
I have given hostnames and I want to preg_match a specific pattern
hostname :
sub1.hostname1.com sub12.hostname2.com suboo2.hostname3.com sub2.hostname4.com
after preg_match
< Strong> desired output :
sub1.hostname1.com suboo2.hostname3.com sub2.hostname4.com
This idea received the hostname In which a 1
or 2
in subdomain
If you have a paper and pen, and you have been asked to write a simple algorithm that Does, how do you do it?
How can I do this?
-
You can determine where the sub-domain is, finding the first dot in the string, therefore, Find and then remove subdomain.
-
Since we are only interested in numbers, logically, we will remove whatever we do not.
-
Now we have only one number, and it should be very simple to evaluate whether we want to. In the code, you can do something like this:
$ samples = ['sub1.hostname1.com', 'sub12. Hostname2.com ',' suboo2.hostname3.com ',' sub2.hostname4.com ',]; Get foreign currency (dollar sample as dollar) {// sub-domain $ dot = strokes ($ domain, '.'); If ($ dot === false) {continue; } $ Sub = substr ($ domain, 0, $ dot); // Remove non-numbers from sub-domain $ number = filter_var ($ sub, FILTER_SANITIZE_NUMBER_INT); // Check what we want if ($ number == 1 or $ number == 2) {resonate "$ domain is a $ number sub-domain & lt; br & gt;"; }}
I will leave it as a drill to throw it into a function, if this is what you are looking for.
If you are completely a regular expression then it will be a matter of examining subdomains for this number:
foreach ($ samples as $ domain) {$ Matches = preg_match ('/ ^ [^ 0- 9] + (?: 1 | 2) \ ./', $ domain); If ($ matches === 1) {echo "$ domain is domain 1 or 2 subdomain
"; }}
Comments
Post a Comment