how to cut a certain group of words in a string? 
November 17th, 2008, 04:45 PM
| | | |
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. | 
November 17th, 2008, 05:15 PM
| | | | 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
-- | 
November 17th, 2008, 05:25 PM
| | | | 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
--
| | 
November 17th, 2008, 05:35 PM
| | | | 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
>--
| >
>
| |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|