Link to home
Start Free TrialLog in
Avatar of amedio
amedio

asked on

Search String

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>';
}
}
Avatar of sapparod
sapparod

Can you post prov.html? and what is the expected result?
Avatar of amedio

ASKER

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
Avatar of kaufmed
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

Avatar of amedio

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America 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
Avatar of amedio

ASKER

The problem was solved, but i need to upgrade information with another function for avoid regular expression use.
Nice to see someone using Opera.