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
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
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
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
Share on other sites

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?

Link to comment
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
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
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.