Jump to content

Preg_replace


mysterbx

Recommended Posts

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

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/87721-preg_replace/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/87721-preg_replace/#findComment-448805
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/87721-preg_replace/#findComment-449025
Share on other sites

<?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);
?>

Link to comment
https://forums.phpfreaks.com/topic/87721-preg_replace/#findComment-449084
Share on other sites

<?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);
?>

Link to comment
https://forums.phpfreaks.com/topic/87721-preg_replace/#findComment-449164
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/87721-preg_replace/#findComment-449244
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.