mysterbx Posted January 25, 2008 Share Posted January 25, 2008 Hi, how can i make preg_replace to return the found text only? examle $text = "<title>my title</title> html body goes down here"; $text = preg_replace("'<title>(.*?)</title>'", '\\1', $text) i get the result, but the whole body still appears... it should look like this: return $text; output: my title Quote Link to comment Share on other sites More sharing options...
PHP Monkeh Posted January 25, 2008 Share Posted January 25, 2008 Hi there, this code should work: <?php $text = "<title>my title</title> html body goes down here"; $title = preg_match("#\<title\>(.*?)\<\/title\>#", $text, $matches); echo $matches[1]; ?> Quote Link to comment Share on other sites More sharing options...
mysterbx Posted January 25, 2008 Author Share Posted January 25, 2008 thanks for the reply, everything worked fine... ... and then I tried this code: $links = preg_match_all("#(^|[\n ])([\w]+?://[^ \"\n\r\t<]*)#ise", $document, $xlinks); print_r($xlinks); when i try to order all links i get: Array ( [0] => Array ( [0] => http://example.com/files/84719507/freefile2.rar [1] => [2] => http://example.com/files/84719507/freefile2.rar ) [1] => Array ( [0] => http://example.com/files/84891355/freefile3.rar [1] => [2] => http://example.com/files/84891355/freefile3.rar ) [2] => Array ( [0] => http://example.com/files/84901878/freefile4.rar [1] => [2] => http://example.com/files/84901878/freefile4.rar ) [3] => Array ( [0] => http://example.com/files/84911171/freefile5.rar [1] => [2] => http://example.com/files/84911171/freefile5.rar ) [4] => Array ( [0] => http://example.com/files/84919648/freefile6.rar [1] => [2] => http://example.com/files/84919648/freefile6.rar ) [5] => Array ( [0] => http://example.com/files/84927124/freefile7.rar [1] => [2] => http://example.com/files/84927124/freefile7.rar ) [6] => Array ( [0] => http://example.com/files/84887713/freefile8.rar [1] => [2] => http://example.com/files/84887713/freefile8.rar ) ) is it possible to order them in a line (nl2br) with no "Array" tags? Quote Link to comment Share on other sites More sharing options...
resago Posted January 25, 2008 Share Posted January 25, 2008 to get the preg_replace to work, the pattern would have been '/.*<title>(.*)<\/title>.*/is' s means look at all lines as one.i means case insensitive. Quote Link to comment Share on other sites More sharing options...
mysterbx Posted January 25, 2008 Author Share Posted January 25, 2008 This one works too thanks what about the other mistery... could you help me on that? Quote Link to comment Share on other sites More sharing options...
PHP Monkeh Posted January 25, 2008 Share Posted January 25, 2008 Which other mystery do you speak of? Quote Link to comment Share on other sites More sharing options...
mysterbx Posted January 25, 2008 Author Share Posted January 25, 2008 thanks for the reply, everything worked fine... ... and then I tried this code: $links = preg_match_all("#(^|[\n ])([\w]+?://[^ \"\n\r\t<]*)#ise", $document, $xlinks); print_r($xlinks); when i try to order all links i get: Array ( [0] => Array ( [0] => http://example.com/files/84719507/freefile2.rar [1] => [2] => http://example.com/files/84719507/freefile2.rar ) [1] => Array ( [0] => http://example.com/files/84891355/freefile3.rar [1] => [2] => http://example.com/files/84891355/freefile3.rar ) [2] => Array ( [0] => http://example.com/files/84901878/freefile4.rar [1] => [2] => http://example.com/files/84901878/freefile4.rar ) [3] => Array ( [0] => http://example.com/files/84911171/freefile5.rar [1] => [2] => http://example.com/files/84911171/freefile5.rar ) [4] => Array ( [0] => http://example.com/files/84919648/freefile6.rar [1] => [2] => http://example.com/files/84919648/freefile6.rar ) [5] => Array ( [0] => http://example.com/files/84927124/freefile7.rar [1] => [2] => http://example.com/files/84927124/freefile7.rar ) [6] => Array ( [0] => http://example.com/files/84887713/freefile8.rar [1] => [2] => http://example.com/files/84887713/freefile8.rar ) ) is it possible to order them in a line (nl2br) with no "Array" tags? How can i remove those Arrays and bullets and not to make double links: Example (should look like this) http://example.com/files/84719507/freefile2.rar http://example.com/files/84891355/freefile3.rar http://example.com/files/84901878/freefile4.rar http://example.com/files/84911171/freefile5.rar http://example.com/files/84919648/freefile6.rar http://example.com/files/84927124/freefile7.rar http://example.com/files/84887713/freefile8.rar Quote Link to comment Share on other sites More sharing options...
PHP Monkeh Posted January 25, 2008 Share Posted January 25, 2008 <?php for($i=0; $i<count($arrayName); $i++) { echo $arrayName[$i]."<br>"; } ?> Replace $arrayName with whatever your array of links is called, which looks like it's $xlinks Quote Link to comment Share on other sites More sharing options...
mysterbx Posted January 25, 2008 Author Share Posted January 25, 2008 maybe this $links = preg_match("#(^|[\n ])([\w]+?://[^ \"\n\r\t<]*)#ise", $document, $xlinks); link searching code is incorrect? Cus if i use the code you have posted, i get "ArrayArrayArray" ... Quote Link to comment Share on other sites More sharing options...
PHP Monkeh Posted January 25, 2008 Share Posted January 25, 2008 Can you show us all the code you have so far? Including the regex and the loop. Quote Link to comment Share on other sites More sharing options...
mysterbx Posted January 25, 2008 Author Share Posted January 25, 2008 <?php function webpage2txt($url) { $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FAILONERROR, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_PORT, 80); curl_setopt($ch, CURLOPT_TIMEOUT, 15); curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); $document = curl_exec($ch); $title = preg_replace('/.*<title>(.*)<\/title>.*/is', '\\1', $document); // THIS LINE NEEDS WORK... $links = preg_match_all("#(^|[\n ])([\w]+?://[^ \"\n\r\t<]*)#ise", $document, $xlinks); // THIS LINE NEEDS WORK... $crawl = "$title<br><br>$xlinks"; return $crawl; } $page = "http://www.wareznova.com/dl$id/-.htm"; echo webpage2txt($page); ?> Quote Link to comment Share on other sites More sharing options...
PHP Monkeh Posted January 25, 2008 Share Posted January 25, 2008 Personally I'm not great at regex, so I can't see what your $links is doing. But I can see that you haven't put my loop in to your code. Quote Link to comment Share on other sites More sharing options...
mysterbx Posted January 25, 2008 Author Share Posted January 25, 2008 I have put your loop, but it didnt give results... $links (should) be searching for links (ftp|http://) but i dont know regex too, and i dont know is it realy searching for them.... maybe there is an other variable whitch would search for links and the show 'em? Quote Link to comment Share on other sites More sharing options...
PHP Monkeh Posted January 25, 2008 Share Posted January 25, 2008 Where abouts did you put the loop in your code? Because you've removed it from that example Quote Link to comment Share on other sites More sharing options...
mysterbx Posted January 25, 2008 Author Share Posted January 25, 2008 <?php function webpage2txt($url) { $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FAILONERROR, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_PORT, 80); curl_setopt($ch, CURLOPT_TIMEOUT, 15); curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); $document = curl_exec($ch); $title = preg_replace('/.*<title>(.*)<\/title>.*/is', '\\1', $document); $links = preg_match_all("#(^|[\n ])([\w]+?://[^ \"\n\r\t<]*)#ise", $document, $xlinks); for($i=0; $i<count($xlinks); $i++) { echo $xlinks[$i]."<br>"; } } $page = "http://www.wareznova.com/dl$id/-.htm"; echo webpage2txt($page); ?> Quote Link to comment Share on other sites More sharing options...
mysterbx Posted January 25, 2008 Author Share Posted January 25, 2008 any ideas? Quote Link to comment Share on other sites More sharing options...
PHP Monkeh Posted January 25, 2008 Share Posted January 25, 2008 I was coming home from work so didn't get a chance to reply. Personally I don't think the preg_match_all() is quite right, because it seems to be returning a multi-dimensional array (which I didn't notice at first, that's why my code was echo-ing ArrayArrayArray). Maybe you could try getting some specific help on that regular expression, because I don't know if it's working how you're wanting it to. Quote Link to comment Share on other sites More sharing options...
mysterbx Posted January 25, 2008 Author Share Posted January 25, 2008 then I should repost this post in "Regex" forum? .. but its already there... Quote Link to comment Share on other sites More sharing options...
PHP Monkeh Posted January 25, 2008 Share Posted January 25, 2008 People tend not to read threads when they're longer than 2 pages long, as they assume people are having an on-going discussion. Try reposting it under a thread where the title suggests finding links within a page. Quote Link to comment Share on other sites More sharing options...
mysterbx Posted January 25, 2008 Author Share Posted January 25, 2008 will try... well thanks for the help... 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.