Solved

Search String

Posted on 2011-03-08
7
360 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

687 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