Connecting Tech Pros Worldwide Forums | Help | Site Map

how to cut a certain group of words in a string?

Paul Jung
Guest
 
Posts: n/a
#1: Nov 17 '08
Hello there,

how to cut a certain group of words in a string, for example,
"<img src='images/smiles/grin.gif' smilietext=':grin:' border='0'
style='vertical-align:middle' alt=':grin:' title=':grin:' />"
"<img src='images/smiles/smile.gif' smilietext=': smile:' border='0'
style='vertical-align:middle' alt=': smile:' title=': smile:' />"
¡*..
from a forum comment.

I tried this:
$subject =$row_Recordset2['pagetext']
$pattern="/<img src='images\/smiles\/[a-zA-Z]+\.gif' smilietext=':[a-zA-Z]:'
border='0' style='vertical-align:middle' alt=':[a-zA-Z]:' title=':[a-zA-Z]:'
\/>/";
$result = preg_replace($pattern,"",$subject);
But I failed, I am new to PHP, please help. Thanks very much in advance.

logically, is it a better way to search for all what is inside the <img ...
/tag (including numbers) and replace them with ""?
I will write: $pattern="/<img [a-zA-Z][0-9]>\/>/", is it correct? Thank you
for your help.



=?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=
Guest
 
Posts: n/a
#2: Nov 17 '08

re: how to cut a certain group of words in a string?


Paul Jung escribió:
Quote:
how to cut a certain group of words in a string, for example,
"<img src='images/smiles/grin.gif' smilietext=':grin:' border='0'
style='vertical-align:middle' alt=':grin:' title=':grin:' />"
"<img src='images/smiles/smile.gif' smilietext=': smile:' border='0'
style='vertical-align:middle' alt=': smile:' title=': smile:' />"
¡*..
from a forum comment.
>
I tried this:
$subject =$row_Recordset2['pagetext']
$pattern="/<img src='images\/smiles\/[a-zA-Z]+\.gif' smilietext=':[a-zA-Z]:'
border='0' style='vertical-align:middle' alt=':[a-zA-Z]:' title=':[a-zA-Z]:'
\/>/";
$result = preg_replace($pattern,"",$subject);
But I failed, I am new to PHP, please help. Thanks very much in advance.
A couple of tips:

- You can strip HTML tags with strip_tags() or filter_var().
- You can sanitize HTML output with a third-party library like HTML
Purifier: http://htmlpurifier.org/

If you merely want to remove <imgtags then...
Quote:
logically, is it a better way to search for all what is inside the <img ...
/tag (including numbers) and replace them with ""?
I will write: $pattern="/<img [a-zA-Z][0-9]>\/>/", is it correct? Thank you
for your help.
.... try something like:

$pattern = '@<img\s.*/?>@Uis'; // Not tested

Just be aware that regexp based HTML parsing tends to fail when faced to
invalid HTML.



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Paul Jung
Guest
 
Posts: n/a
#3: Nov 17 '08

re: how to cut a certain group of words in a string?


WOW!
thank you so much!
didn't hear strip HTML tags before, will google it.....
so far, thank you and have a nice evening!

Paul


""Álvaro G. Vicario"" <alvaroNOSPAMTHANKS@demogracia.com>
??????:gfs87q$cd8$1@huron.algomas.org...
Quote:
Paul Jung escribió:
Quote:
>how to cut a certain group of words in a string, for example,
>"<img src='images/smiles/grin.gif' smilietext=':grin:' border='0'
>style='vertical-align:middle' alt=':grin:' title=':grin:' />"
>"<img src='images/smiles/smile.gif' smilietext=': smile:' border='0'
>style='vertical-align:middle' alt=': smile:' title=': smile:' />"
>¡*..
>from a forum comment.
>>
>I tried this:
>$subject =$row_Recordset2['pagetext']
>$pattern="/<img src='images\/smiles\/[a-zA-Z]+\.gif'
>smilietext=':[a-zA-Z]:' border='0' style='vertical-align:middle'
>alt=':[a-zA-Z]:' title=':[a-zA-Z]:' \/>/";
>$result = preg_replace($pattern,"",$subject);
>But I failed, I am new to PHP, please help. Thanks very much in advance.
>
A couple of tips:
>
- You can strip HTML tags with strip_tags() or filter_var().
- You can sanitize HTML output with a third-party library like HTML
Purifier: http://htmlpurifier.org/
>
If you merely want to remove <imgtags then...
>
Quote:
>logically, is it a better way to search for all what is inside the <img
>... /tag (including numbers) and replace them with ""?
>I will write: $pattern="/<img [a-zA-Z][0-9]>\/>/", is it correct? Thank
>you for your help.
>
... try something like:
>
$pattern = '@<img\s.*/?>@Uis'; // Not tested
>
Just be aware that regexp based HTML parsing tends to fail when faced to
invalid HTML.
>
>
>
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--

Paul Jung
Guest
 
Posts: n/a
#4: Nov 17 '08

re: how to cut a certain group of words in a string?


>... try something like:
Quote:
Quote:
>>
>$pattern = '@<img\s.*/?>@Uis'; // Not tested
IT WORKS GREAT!!!
WOW!!!
THANKS!!!


"Paul Jung" <etjsk@hotmail.comдÈëÏûÏ¢ÐÂÎÅ:b5365$4921a79d$55d8 b761$1851@news.chello.sk...
Quote:
WOW!
thank you so much!
didn't hear strip HTML tags before, will google it.....
so far, thank you and have a nice evening!
>
Paul
>
>
""Álvaro G. Vicario"" <alvaroNOSPAMTHANKS@demogracia.com>
??????:gfs87q$cd8$1@huron.algomas.org...
Quote:
>Paul Jung escribi?
Quote:
>>how to cut a certain group of words in a string, for example,
>>"<img src='images/smiles/grin.gif' smilietext=':grin:' border='0'
>>style='vertical-align:middle' alt=':grin:' title=':grin:' />"
>>"<img src='images/smiles/smile.gif' smilietext=': smile:' border='0'
>>style='vertical-align:middle' alt=': smile:' title=': smile:' />"
>>¡*..
>>from a forum comment.
>>>
>>I tried this:
>>$subject =$row_Recordset2['pagetext']
>>$pattern="/<img src='images\/smiles\/[a-zA-Z]+\.gif'
>>smilietext=':[a-zA-Z]:' border='0' style='vertical-align:middle'
>>alt=':[a-zA-Z]:' title=':[a-zA-Z]:' \/>/";
>>$result = preg_replace($pattern,"",$subject);
>>But I failed, I am new to PHP, please help. Thanks very much in advance.
>>
>A couple of tips:
>>
>- You can strip HTML tags with strip_tags() or filter_var().
>- You can sanitize HTML output with a third-party library like HTML
>Purifier: http://htmlpurifier.org/
>>
>If you merely want to remove <imgtags then...
>>
Quote:
>>logically, is it a better way to search for all what is inside the <img
>>... /tag (including numbers) and replace them with ""?
>>I will write: $pattern="/<img [a-zA-Z][0-9]>\/>/", is it correct? Thank
>>you for your help.
>>
>... try something like:
>>
>$pattern = '@<img\s.*/?>@Uis'; // Not tested
>>
>Just be aware that regexp based HTML parsing tends to fail when faced to
>invalid HTML.
>>
>>
>>
>--
>-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
>-- Mi sitio sobre programación web: http://bits.demogracia.com
>-- Mi web de humor al baño María: http://www.demogracia.com
>--
>
>

Closed Thread