• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

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>';
}
}
0
amedio
Asked:
amedio
1 Solution
 
sapparodCommented:
Can you post prov.html? and what is the expected result?
0
 
amedioAuthor Commented:
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
 
käµfm³d 👽Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
amedioAuthor Commented:
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
 
käµfm³d 👽Commented:
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
 
amedioAuthor Commented:
The problem was solved, but i need to upgrade information with another function for avoid regular expression use.
0
 
Lukasz ChmielewskiCommented:
Nice to see someone using Opera.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now