Page 1 of 1

help about text between and replace it

Posted: Sat Jan 23, 2010 8:21 am
by fer0an
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:

Re: help about text between and replace it

Posted: Sat Jan 23, 2010 10:14 am
by SimpleManWeb
Here is some example code that I wrote a while ago. You will need to make some changes, but this should get you on the right path.

Code: Select all

 
        $Start = strpos($Text, "href="); //Look for the being of what you want to replace
        $Start += 6;
        $Temp = substr($Text, $Start); //Narrow the string down to the area that you want to replace
        $End = strpos($Temp, '">'); //Now, find out the position of when you want to stop replacing
        $Replacement= substr($Text, $Start, $End); //Find out exactly what text is between the start and the end
                $NewText = "The New Text Here";
        str_replace($Replacement, $NewText ,$Text); //Change your text. 
 
 
Hope this helps

Re: help about text between and replace it

Posted: Sat Jan 23, 2010 11:34 am
by AbraCadaver
fer0an wrote: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 :

anyone can help me? :cry:
What!?!? Show the HTML that you want to replace and what you want to replace it with.

Re: help about text between and replace it

Posted: Sat Jan 23, 2010 2:46 pm
by fer0an
hi
this is html code :

'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

and I want remove href="http://domain.com/tag...."> or select this codes and replace with nothing.

thank you

Re: help about text between and replace it

Posted: Sat Jan 23, 2010 3:36 pm
by AbraCadaver

Code: Select all

$title = preg_replace('/href[^>]*>/', '', $title);

Re: help about text between and replace it

Posted: Sun Jan 24, 2010 1:13 am
by fer0an
thank you
but I recive an error :

Warning: Wrong parameter count for preg_replace() in /home/...

Re: help about text between and replace it

Posted: Sun Jan 24, 2010 9:21 am
by AbraCadaver
Sorry, too fast typing. I edited the post above.