djingel Posted January 18, 2009 Share Posted January 18, 2009 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? Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted January 18, 2009 Share Posted January 18, 2009 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. Quote Link to comment Share on other sites More sharing options...
djingel Posted January 18, 2009 Author Share Posted January 18, 2009 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 :-) Quote Link to comment Share on other sites More sharing options...
.josh Posted January 18, 2009 Share Posted January 18, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.