Jump to content

select text between and replace with


fer0an

Recommended Posts

hello

I want select text between some html code and then replace it by ''.( replace from  'href' until '">' then replace this text with '')

this is my code :

 

<?php

set_time_limit(0) ;

ob_implicit_flush(true);

ob_end_flush();

function TextBetween($s1,$s2,$s){

$s1 = strtolower($s1);

$s2 = strtolower($s2);

$L1 = strlen($s1);

$scheck = strtolower($s);

if($L1>0){$pos1 = strpos($scheck,$s1);} else {$pos1=0;}

if($pos1 !== false){

  if($s2 == '') return substr($s,$pos1+$L1);

  $pos2 = strpos(substr($scheck,$pos1+$L1),$s2);

  if($pos2!==false) return substr($s,$pos1+$L1,$pos2);

 

}

 

return '';

 

}

 

 

$title = 'Jornal O Estado de SP em href="http://domain.com/tag/pdf/" class="st_tag internal_tag" rel="tag" title="Posts tagged with pdf">PDF, S&#225;bado, 23 de href="http://domain.com/tag/janeiro/" class="st_tag internal_tag" rel="tag" title="Posts tagged with janeiro">Janeiro de 2010

href="http://domain.com/tag/pdf/" class="st_tag internal_tag" rel="tag" title="Posts tagged with pdf">PDF | href="http://domain.com/tag/brazil/" class="st_tag internal_tag" rel="tag" title="Posts tagged with brazil">Brazil | 84 href="http://domain.com/tag/pages/" class="st_tag internal_tag" rel="tag" title="Posts tagged with pages">Pages | 32 MB';

 

 

$title3 = textbetween('href="','">',$title);

while ($title3 != "")

{

$title2 = str_ireplace ($title3,'',$title);

 

echo $title2."<br>" ;

}

echo $title2."<br>" ;

?>

 

 

anyone can help me? :cry:

Link to comment
https://forums.phpfreaks.com/topic/189533-select-text-between-and-replace-with/
Share on other sites

hi

$title3 = textbetween('href="','">',$title) so until it's have some char $title2 = str_ireplace ($title3,'',$title); so replace html codes with nothing.

the problem is

after run codes only first textbetween replace and for another nothing happen.

I want after run this codes all of textbetween replace with nothing.

 

thank you

I tried that, with this simple script:

<?php
$title = 'Jornal O Estado de SP em href="http://domain.com/tag/pdf/" class="st_tag internal_tag" rel="tag" title="Posts tagged with pdf">PDF, Sábado, 23 de href="http://domain.com/tag/janeiro/" class="st_tag internal_tag" rel="tag" title="Posts tagged with janeiro">Janeiro de 2010
href="http://domain.com/tag/pdf/" class="st_tag internal_tag" rel="tag" title="Posts tagged with pdf">PDF | href="http://domain.com/tag/brazil/" class="st_tag internal_tag" rel="tag" title="Posts tagged with brazil">Brazil | 84 href="http://domain.com/tag/pages/" class="st_tag internal_tag" rel="tag" title="Posts tagged with pages">Pages | 32 MB';

echo $title=preg_replace('/href=".*?"/','',$title);
?>

 

And it worked as expected

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.