Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: [PHP] Removing link on the fly, but leave link text

0 views
Skip to first unread message

Karl DeSaulniers

unread,
Aug 31, 2010, 4:07:49 PM8/31/10
to php-general

On Aug 31, 2010, at 2:34 PM, Joshua Kehn wrote:

> On Aug 31, 2010, at 3:31 PM, Karl DeSaulniers wrote:
>
>> Hi,
>> Say I have some text.
>>
>> $text = 'You can logon here: <a href="http://website.com/shop/
>> index.php?username='.$username.'">http://website.com/shop/
>> index.php?username='.$username.'</a>. This link will take you to
>> your web browser to login.'.$eol;
>>
>> I want to be able to strip the "<a href="http://website.com/shop/
>> index.php?username='.$username.'">" and </a>.
>> Leaving just the http://website.com/shop/index.php?username='.
>> $username.' text, so it would end up like.
>>
>>
>> $text = 'You can logon here: http://website.com/shop/index.php?
>> username='.$username.'. This link will take you to your web
>> browser to login.'.$eol;
>>
>> I have tried MANY different ways and have no success.
>> Can anyone help me?
>> TIA
>>
>>
>> Karl DeSaulniers
>> Design Drumm
>> http://designdrumm.com
>>
>
> Would it be safe to simply strip out anything between < and > ?
>
> Regards,
>
> -Josh
> ____________________________________
> Joshua Kehn | Josh...@gmail.com
> http://joshuakehn.com
>


Hi Josh,

Safe? Not sure what you mean.
The text is for an email that is originally done in html code,
that when sent, checks to see if the recipiant can get html emails.
if not the text version is sent, but I want to remove any <a href""></
a> tags before sending.

This is how it starts:
$text = 'You can logon here: <a href="http://website.com/shop/
index.php?username='.$username.'">http://website.com/shop/index.php?
username='.$username.'</a>. This link will take you to your web
browser to login.'.$eol;
This is how I want it if they dont get the thml version
$text = 'You can logon here: http://website.com/shop/index.php?
username='.$username.'. This link will take you to your web browser
to login.'.$eol;

Hope that clarifies
Best,

Karl DeSaulniers
Design Drumm
http://designdrumm.com

Phpster

unread,
Aug 31, 2010, 10:16:01 PM8/31/10
to a...@ashleysheridan.co.uk, Jo?o C?ndido de Souza Neto, php-g...@lists.php.net

On Aug 31, 2010, at 16:26, Ashley Sheridan <a...@ashleysheridan.co.uk> wrote:

> On Tue, 2010-08-31 at 17:15 -0300, Jo?o C?ndido de Souza Neto wrote:
>
>> Theres an eror.
>>
>> The correct is:
>>
>> $pattern = array(
>> "/<a [^>]>/",
>> "/</a>/"
>> );
>>
>> $text = preg_replace($pattern, "", $text);
>>
>>
>> --
>> Joo Cndido de Souza Neto
>>
>> ""Joo Cndido de Souza Neto"" <jo...@consultorweb.cnt.br> escreveu na
>> mensagem news:D0.73.4895...@pb1.pair.com...
>>> Try this:
>>>
>>> $pattern = array(
>>> "/<a [^>]/",
>>> "/</a>/"
>>> );
>>>
>>> preg_replace($pattern, "", $text);
>>>
>>> --
>>> Joo Cndido de Souza Neto
>>>
>>> "Karl DeSaulniers" <ka...@designdrumm.com> escreveu na mensagem
>>> news:007BC8E1-B2C8-4DD5...@designdrumm.com...


>>>> Hi,
>>>> Say I have some text.
>>>>
>>>> $text = 'You can logon here: <a href="http://website.com/shop/

>>>> index.php?username='.$username.'">http://website.com/shop/index.php?
>>>> username='.$username.'</a>. This link will take you to your web
>>>> browser to login.'.$eol;
>>>>

>>>> I want to be able to strip the "<a href="http://website.com/shop/
>>>> index.php?username='.$username.'">" and </a>.
>>>> Leaving just the http://website.com/shop/index.php?username='.
>>>> $username.' text, so it would end up like.
>>>>
>>>>
>>>> $text = 'You can logon here: http://website.com/shop/index.php?
>>>> username='.$username.'. This link will take you to your web browser
>>>> to login.'.$eol;
>>>>
>>>> I have tried MANY different ways and have no success.
>>>> Can anyone help me?
>>>> TIA
>>>>
>>>>
>>>> Karl DeSaulniers
>>>> Design Drumm
>>>> http://designdrumm.com
>>>>
>>>>
>>>
>>>
>>
>>
>>
>
>

> Regular expressions are evil when used like this. Consider the following
> valid HTML:
>
> <a href="math_operators.php" title="The > Operator">The &gt;
> Operator</a>
>
> The HTML is perfectly valid, but the regex will break because it "see's"
> the end of the tag prematurely from the > inside the title attribute
> value.
>
> However, if the HTML is going to be a very small subset in which you
> will always know this sort of thing won't happen, then you could use the
> regex.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>

Karl,

Is this for an email?

Bastien

Sent from my iPod

Ashley Sheridan

unread,
Aug 31, 2010, 4:06:22 PM8/31/10
to Karl DeSaulniers, php-general
On Tue, 2010-08-31 at 15:01 -0500, Karl DeSaulniers wrote:

> On Aug 31, 2010, at 2:32 PM, Ashley Sheridan wrote:


>
> > On Tue, 2010-08-31 at 14:31 -0500, Karl DeSaulniers wrote:
> >>
> >> Hi,
> >> Say I have some text.
> >>
> >> $text = 'You can logon here: <a href="http://website.com/shop/
> >> index.php?username='.$username.'">http://website.com/shop/index.php?
> >> username='.$username.'</a>. This link will take you to your web
> >> browser to login.'.$eol;
> >>
> >> I want to be able to strip the "<a href="http://website.com/shop/
> >> index.php?username='.$username.'">" and </a>.
> >> Leaving just the http://website.com/shop/index.php?username='.
> >> $username.' text, so it would end up like.
> >>
> >>
> >> $text = 'You can logon here: http://website.com/shop/index.php?
> >> username='.$username.'. This link will take you to your web browser
> >> to login.'.$eol;
> >>
> >> I have tried MANY different ways and have no success.
> >> Can anyone help me?
> >> TIA
> >>
> >>
> >> Karl DeSaulniers
> >> Design Drumm
> >> http://designdrumm.com
> >>
> >

> > strip_tags() will do the job here.
> >
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> >
>
>
> Hi Ash,
> I tried that, but it did not work.
> I do not want it to strip all tags, just the <a href=""></a>. so if
> there was a <p> or <br />. I don't want those removed.
> There is more text than just what I posted.
>
> This is what I am trying currently.
> This is someones code from a forum that I am trying to adopt.
>
> //CODE start
> function strip_only($str, $tags) { // Thanks Steve
> if(!is_array($tags)) {
> $tags = (strpos($str, '>') !== false ? explode('>',
> str_replace('<', '', $tags)) : array($tags));
> if(end($tags) == '') array_pop($tags);
> }
> foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]
> *>#is', '', $str);
> return $str;
> }
>
> $str = '<p style="text-align:center">Paragraph</p><strong>Bold</
> strong><br/><span style="color:red">Red</span><h1>Header</h1>';
>
> echo strip_only($str, array('p', 'h1'));
> echo strip_only($str, '<p><h1>');
>
> //CODE end
>
> I was using it like so.
>
> $text = strip_only($text, array('a'));
> or
> $text = strip_only($text, '<a>');
>
> But got my results back like:
> 'You can logon here: href="http://website.com/shop/
> index.php?username='.$username.'">http://website.com/shop/index.php?
> username='.$username.'. This link will take you to your web
> browser to login.'.$eol;
> uugh..


>
> Karl DeSaulniers
> Design Drumm
> http://designdrumm.com
>


Look at the second argument to strip_tags() which allows you to specify
a list of allowable tags. It will do what you want, you just need to
look at the manual.

Thanks,
Ash
http://www.ashleysheridan.co.uk


Karl DeSaulniers

unread,
Aug 31, 2010, 4:01:59 PM8/31/10
to php-general

João Cândido de Souza Neto

unread,
Aug 31, 2010, 4:11:11 PM8/31/10
to php-g...@lists.php.net
Try this:

$pattern = array(
"/<a [^>]/",
"/</a>/"
);

preg_replace($pattern, "", $text);

--
João Cândido de Souza Neto

"Karl DeSaulniers" <ka...@designdrumm.com> escreveu na mensagem
news:007BC8E1-B2C8-4DD5...@designdrumm.com...

Karl DeSaulniers

unread,
Aug 31, 2010, 3:31:53 PM8/31/10
to php-general

Jim Lucas

unread,
Aug 31, 2010, 11:48:57 PM8/31/10
to a...@ashleysheridan.co.uk, Jo?o C?ndido de Souza Neto, php-g...@lists.php.net
Ashley Sheridan wrote:
> On Tue, 2010-08-31 at 17:15 -0300, Jo?o C?ndido de Souza Neto wrote:
>
>> Theres an eror.
>>
>> The correct is:
>>
>> $pattern = array(
>> "/<a [^>]>/",
>> "/</a>/"
>> );
>>
>> $text = preg_replace($pattern, "", $text);
>>
>>
>> --
>> Joo Cndido de Souza Neto
>>
>> ""Joo Cndido de Souza Neto"" <jo...@consultorweb.cnt.br> escreveu na
>> mensagem news:D0.73.4895...@pb1.pair.com...
>>> Try this:
>>>
>>> $pattern = array(
>>> "/<a [^>]/",
>>> "/</a>/"
>>> );
>>>
>>> preg_replace($pattern, "", $text);
>>>
>>> --
>>> Joo Cndido de Souza Neto
> Regular expressions are evil when used like this. Consider the following
> valid HTML:
>
> <a href="math_operators.php" title="The > Operator">The &gt;
> Operator</a>
>
> The HTML is perfectly valid, but the regex will break because it "see's"
> the end of the tag prematurely from the > inside the title attribute
> value.
>
> However, if the HTML is going to be a very small subset in which you
> will always know this sort of thing won't happen, then you could use the
> regex.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>

Here is the trail that I just went down.

http://www.w3.org/TR/html401/struct/links.html
lead me to here
http://www.w3.org/TR/html401/struct/global.html#adef-title
which lead me to here
http://www.w3.org/TR/html401/types.html#type-text
which lead me to here
http://www.w3.org/TR/html401/sgml/dtd.html#Text
which lead me to here
http://www.w3.org/TR/html401/types.html#type-cdata

To my final conclusion that values of attributes, although it isn't
required, should be encoded and conversely will be decoded by the client.

So, not to say that you are wrong, but I would suggest that it might be
better to explain that the example you suggested should have been
written in the following way instead.

<a href="math_operators.php" title="The &gt; Operator">The &gt;
Operator</a>

All text that gets sent to the client, that has nothing to do with
layout, should be ran through some type of cleaning & encoding method
before being sent to the client.

Just my 2pennies...

Jim Lucas

Karl DeSaulniers

unread,
Aug 31, 2010, 4:12:50 PM8/31/10
to php-general


I did and it seemed like the long way around.
I dont need/want to set all the tags that are allowed.
I just need to set the one that isnt.
But thank you, I will take another look at strip_tags().
Best,

Ashley Sheridan

unread,
Aug 31, 2010, 3:32:29 PM8/31/10
to Karl DeSaulniers, php-general
On Tue, 2010-08-31 at 14:31 -0500, Karl DeSaulniers wrote:

João Cândido de Souza Neto

unread,
Aug 31, 2010, 4:15:32 PM8/31/10
to php-g...@lists.php.net
There´s an eror.

The correct is:

$pattern = array(


"/<a [^>]>/",
"/</a>/"
);

$text = preg_replace($pattern, "", $text);


--
João Cândido de Souza Neto

""João Cândido de Souza Neto"" <jo...@consultorweb.cnt.br> escreveu na
mensagem news:D0.73.4895...@pb1.pair.com...

Ashley Sheridan

unread,
Aug 31, 2010, 4:26:08 PM8/31/10
to Jo?o C?ndido de Souza Neto, php-g...@lists.php.net
On Tue, 2010-08-31 at 17:15 -0300, Jo?o C?ndido de Souza Neto wrote:

> Theres an eror.


>
> The correct is:
>
> $pattern = array(
> "/<a [^>]>/",
> "/</a>/"
> );
>
> $text = preg_replace($pattern, "", $text);
>
>
> --

> Joo Cndido de Souza Neto
>

> ""Joo Cndido de Souza Neto"" <jo...@consultorweb.cnt.br> escreveu na

> mensagem news:D0.73.4895...@pb1.pair.com...
> > Try this:
> >
> > $pattern = array(
> > "/<a [^>]/",
> > "/</a>/"
> > );
> >
> > preg_replace($pattern, "", $text);
> >
> > --

> > Joo Cndido de Souza Neto


> >
> > "Karl DeSaulniers" <ka...@designdrumm.com> escreveu na mensagem
> > news:007BC8E1-B2C8-4DD5...@designdrumm.com...
> >> Hi,
> >> Say I have some text.
> >>
> >> $text = 'You can logon here: <a href="http://website.com/shop/
> >> index.php?username='.$username.'">http://website.com/shop/index.php?
> >> username='.$username.'</a>. This link will take you to your web
> >> browser to login.'.$eol;
> >>
> >> I want to be able to strip the "<a href="http://website.com/shop/
> >> index.php?username='.$username.'">" and </a>.
> >> Leaving just the http://website.com/shop/index.php?username='.
> >> $username.' text, so it would end up like.
> >>
> >>
> >> $text = 'You can logon here: http://website.com/shop/index.php?
> >> username='.$username.'. This link will take you to your web browser
> >> to login.'.$eol;
> >>
> >> I have tried MANY different ways and have no success.
> >> Can anyone help me?
> >> TIA
> >>
> >>
> >> Karl DeSaulniers
> >> Design Drumm
> >> http://designdrumm.com
> >>
> >>
> >
> >
>
>
>

Karl DeSaulniers

unread,
Sep 1, 2010, 8:46:41 PM9/1/10
to php-general


Hey Ash,
I did as you suggested and still got the same results.

Results:


'You can logon here: href="http://website.com/shop/
index.php?username='.$username.'">http://website.com/shop/index.php?
username='.$username.'. This link will take you to your web
browser to login.'.$eol;

Could it be because there is a "?" in the string or the php variable?

0 new messages