Solved

Search String

Posted on 2011-03-08
7
352 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
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 74

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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 74

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now