Jump to content

Recommended Posts

Hi guys,

 

I am quite new to PHP, but I am trying  ???

 

This is what I would like to do. I have a whole website as output in html, but I just want the text between <div id="result"> to see on my screen.

 

This is the code I use for that

preg_match('<div id="result"><div[^>]+>(.+?)</div>',$output,$translated);

 

(it is a translation of a word)

 

this is the error code I receive

Warning: preg_match() [function.preg-match]: Unknown modifier '<' in

 

any idea what I am missing here?

 

Link to comment
https://forums.phpfreaks.com/topic/141326-preg_match-problem/
Share on other sites

What you have to understand is that preg makes use of 'delimiters', which are opening and closing (and usually matching) characters..

Delimiters can be any non-alphanumeric, or non white-space character (except backslashes).. so here are some valid examples:

 

'#....#'

'!......!'

'~.....~'

 

You can use say by example < or [, but then must close it on the otherside with the appropriate closing characters > or ]... But one must be careful in using those, as this means, if you use <......> (expecting those characters to represent characters in the construction of a tag, you are in for a surprise, as since these are valid delimiters, you will only get what is inside these are your actual pattern.. So in your case, to make this valid, you could do this for instance:

 

preg_match('#<div id="result"><div[^>]+>(.+?)</div>#',$output,$translated); Otherwise, since you started with <, the first > character the regex engine runs into, it considers this the closing delimiter.. EDIT - but this is case, there is more after the first > character, thus not legal (nor what you want). You can read more about delimiters here.

 

Link to comment
https://forums.phpfreaks.com/topic/141326-preg_match-problem/#findComment-739696
Share on other sites

Thnx for your reply!

 

However, I think I am losing it here, I should never promised myself to get this script working before I am going to sleep  :-\

 

Now I have the code, but it still gives me the whole html page as output. I only need the translation...

 

Anyone has an idea why?

 

$data = mysql_query ('SELECT word FROM wordlist LIMIT 0,30') or die ('Error: '.mysql_error ());

 

while ($row = mysql_fetch_array($data)) {

  $ch = curl_init("http://babelfish.yahoo.com/translate_txt?ei=UTF-8&doit=done&fr=bf-home&intl=1&tt=urltext&trtext=".urlencode($row[0])."&lp=en_it&btnTrTxt=Translate");

$output = curl_exec($ch); // perform the request

 

preg_match('#<div id="result"><div[^>]+>(.+?)</div>#',$output,$translated); // strip out everything but the translation 

 

}

 

?>

 

Thnx for your help guys, programming is really a nice way to spend time :-)

Link to comment
https://forums.phpfreaks.com/topic/141326-preg_match-problem/#findComment-739749
Share on other sites

You need to post an example of the string you are trying to regex.  There's no way we can tell you what's wrong with your regex without knowing what the subject of the regex is.  What I can do is tell you what your current regex is doing.

 

#<div id="result"><div[^>]+>(.+?)</div>#

 

It is looking for a div tag that looks exactly like this: "<div id="result">"  If there is anything else in that tag or if the format is different in any way that that literal text, it will not match.  If it matches, it then looks for a div tag immediately after it (no whitespace, new lines, other text, nothing).  This 2nd div tag can have anything in it.  If it gets this far, then it will capture everything up to the first </div> it encounters.

Link to comment
https://forums.phpfreaks.com/topic/141326-preg_match-problem/#findComment-739822
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.