Link to home
Start Free TrialLog in
Avatar of Nura111
Nura111

asked on

regex specific issue

Im trying to add a <br> between the word to the phone number and eignore the white spaces

so far I manage to do:

       $buffer = preg_replace('/(<div([^>]+)id="name"(.*?)word)/','$1<br>', $buffer);

I cant figure out how to eliminate the spaces I tried several things and it didnt work

If it possible that the soloution will be explained for the new regex chars

Thank you
div id="name">text word      (111) 111-1348</div>

Open in new window

Avatar of ozo
ozo
Flag of United States of America image

$buffer = preg_replace('/(<div([^>]+id="name".*?)word\s*/','$1<br>word', $buffer);
Not everything can be explained by the God particle and not every problem can be solved by a regular expression.

Here is how I would go about the task.  First, normalize the phone number.  Next create the div tag, the text word, and the normalized phone number.

If you are trying to extract phone numbers from some other web site that is not under your control, please post the URL of the page you're trying to scrape and I will show you how to get the phone numbers.
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sidebar note: This article may appear to be about regular expressions, but it is really about creating test data and using it in thoughtful ways.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
If you can solve your problems without using regular expressions, then don't use them. Regex functions are slower than their php counterparts.
Avatar of Nura111
Nura111

ASKER

re-searcher. is there something I could have done here without using regex?
please, tell me an example.
Avatar of Nura111

ASKER

the exmple is the question I asked so you comment if I can solve it withouir regex I should...
@Nura111: The best thing to do would be to post some examples of the inputs you have and the outputs you want.  That way you would give us the tools we need to show you good solutions.  I am guessing that you might want to do this more than once, so a generalized solution might be helpful.  If we can see the original data source we might be better able to help you.  Did you read the article about Test-Driven Development yet?
Avatar of Nura111

ASKER

yes I did. but in this case my input is always the same and I added it to the question.
I already got here the solution but I was responding to re-searcher comment that regex use is slow and if there is a different way to do that I should so I want to know if there is a different way
Thank you.
Transfer resource usage from server side to client side.
add following javascript code to your html
$(document).ready(function () {
    $('body').html( $('body').html().replace(/(\d\d\d-\d\d\d-\d\d\d\d)/g,'<br>$1') );
});

Open in new window


it's have same functionality. and will replace
123-123-1234

Open in new window

to
<br>123-123-1234

Open in new window

Avatar of Nura111

ASKER

oh ok the problem is the script will work on a mobile site so thats depend if the mobile is supporting javascript right?
i just said another way.

ereg and regex is slower than str_replace and other php functions.
try str_replace.
are* slower...
But, if I Develop something with this subject. I save tel numbers without patterns. i.e +xx (xxx) xxx-xxxx
instead of it I'll remove all patterns before SQL/MySQL/Oracle (DB) inserts.
and after that time I retrieved non-patterned tel numbers, I'll add my own patterns as my choice.
I will use 2 more lines but it will spend less resource usages and time....
 
@Nura111, in case you have a question about how to filter and validate telephone numbers, please post it here, and be sure to mention "telephone number" in the title.  Good luck with your project, ~Ray