Solved

Search String

Posted on 2011-03-08
7
359 Views
Last Modified: 2012-05-11
Hi, i´m trying to searh a couple of words in a String that has been gather from an HTML document file. This code doesn´t work, i think the problem is in the accent (character over the é), anyway i think is better to use some function or expression that searchs the exactly two words in the string, because probably the word "Datos" appears in other part of the HTML...
Thanks a lot for your help.

// the function file_get_html and $element->plaintext is gather from the "Simple HTML DOM" class
$url = "prov.html";
$data = file_get_html($url);
foreach($data->find('a') as $element)
{
// Regular expressions for clean spaces in the string
                  $pattern = '/( )+$/';
                  $string = preg_replace($pattern, '', $element->plaintext);
                  $pattern = '/^( )+/';
                  $string = preg_replace($pattern, '', $string);
if ($string != 'Datos Técnicos')
{
echo 'NO' .  '<br>';
}
else
{
echo 'YES' . '<br>';
}
}
0
Comment
Question by:amedio
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Expert Comment

by:sapparod
ID: 35068096
Can you post prov.html? and what is the expected result?
0
 

Author Comment

by:amedio
ID: 35068145
I attach the file
The string for search is "Datos Técnicos" in the line 518...

The expected result if make another functions when i found the string:

if ($string != 'Datos Técnicos')
{
echo 'NO' .  '<br>';
}
else
{
echo 'YES' . '<br>';
}
}

Thank you very much.
prov.html
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35068480
I don't see any "&nbsp;" in the sample you provided. For fun, though, let's try modifying the pattern a little to see if you get better results. Try the following--note, only the one replace function call should be needed (which is why I only posted one!):
// Regular expressions for clean spaces in the string
$pattern = '/^(?: |&nbsp;)+|(?: |&nbsp;)+$/';
$string = preg_replace($pattern, '', $element->plaintext);

Open in new window

0
Technology Partners: 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!

 

Author Comment

by:amedio
ID: 35068693
The problem continues, i used this regular expressions for another searches criterias that appears the &nbsp, but i think in this case this is not neccesary, because like you say in this line doesn´t appears the &nbsp.

I think i need to improve the "if ($string != 'Datos Técnicos')" with some function or expression
like if (preg_match('/^' . 'Datos Técnicos . '/', $string) && preg_match('/' . 'Datos Técnicos . '$/', $string))

I´m searching something similar without regular expressions, but i don´t find it...

Thank you anyway.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35069124
I was posting earlier from a machine which did not have PHP, but I have now tested the following on a machine which does. See if this suits you better:
// Regular expressions for clean spaces in the string
$pattern = '/^(?:\s|&nbsp;)+|(?:\s|&nbsp;)+$/';
$string = preg_replace($pattern, '', $element->plaintext);

Open in new window

untitled.PNG
0
 

Author Closing Comment

by:amedio
ID: 35070355
The problem was solved, but i need to upgrade information with another function for avoid regular expression use.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35071345
Nice to see someone using Opera.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Find out what you should include to make the best professional email signature for your organization.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 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