?
Solved

regex specific issue

Posted on 2011-10-25
16
Medium Priority
?
236 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:Nura111
  • 6
  • 4
  • 4
  • +2
16 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 37028609
$buffer = preg_replace('/(<div([^>]+id="name".*?)word\s*/','$1<br>word', $buffer);
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37028689
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.
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 total points
ID: 37028691
      $buffer = preg_replace('/(<div[^>]+id="name".*?)\s*(word\s?)\s*/','$1<br>$2', $buffer);

My understanding of your requirements is that for your example you want the output:
<div id="name">text<br>word (111) 111-1348</div>

If not, please indicate your desired output. Also, if you won't always know what "word" will be, we can adjust the pattern to suit.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37028709
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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
0
 
LVL 3

Expert Comment

by:re-searcher
ID: 37042944
If you can solve your problems without using regular expressions, then don't use them. Regex functions are slower than their php counterparts.
0
 

Author Comment

by:Nura111
ID: 37046129
re-searcher. is there something I could have done here without using regex?
0
 
LVL 3

Expert Comment

by:re-searcher
ID: 37046147
please, tell me an example.
0
 

Author Comment

by:Nura111
ID: 37046162
the exmple is the question I asked so you comment if I can solve it withouir regex I should...
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37046167
@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?
0
 

Author Comment

by:Nura111
ID: 37046198
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.
0
 
LVL 3

Expert Comment

by:re-searcher
ID: 37046253
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

0
 

Author Comment

by:Nura111
ID: 37046266
oh ok the problem is the script will work on a mobile site so thats depend if the mobile is supporting javascript right?
0
 
LVL 3

Expert Comment

by:re-searcher
ID: 37046285
i just said another way.

ereg and regex is slower than str_replace and other php functions.
try str_replace.
0
 
LVL 3

Expert Comment

by:re-searcher
ID: 37046490
are* slower...
0
 
LVL 3

Expert Comment

by:re-searcher
ID: 37046622
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....
 
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37046854
@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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Introduction to Processes
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question