473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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.
Nov 17 '08 #1
3 3361
Paul Jung escribió:
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...
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
--
Nov 17 '08 #2
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"" <al****************@demogracia.com>
??????:gf**********@huron.algomas.org...
Paul Jung escribió:
>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...
>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
--

Nov 17 '08 #3
>... try something like:
>>
$pattern = '@<img\s.*/?>@Uis'; // Not tested
IT WORKS GREAT!!!
WOW!!!
THANKS!!!
"Paul Jung" <et***@hotmail.comдÈëÏûÏ¢ÐÂÎÅ:b5***************** *********@news.chello.sk...
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"" <al****************@demogracia.com>
??????:gf**********@huron.algomas.org...
>Paul Jung escribi?
>>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...
>>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
--


Nov 17 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Mosher | last post by:
Hi all, I was wondering if php can parse a text string for certain words and return "true" if that word is found. For example, I have a string like this: $string = "The rain in spain is the same...
18
by: prasanna.hariharan | last post by:
Hi guys, I want to remove certain words from a c++ string. The list of words are in a file with each word in a new line. I tried using the std::transform, but it dint work. Anybody got a clue...
0
by: eSapient | last post by:
For the following XSD: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">...
9
by: PamelaDV | last post by:
I have two problems, actually. I am looking to see if there is a function that will return the day of the week (Monday, Tuesday, Wednesday, etc...) from a date. For instance 2/27/04 is a Friday....
3
by: Sashafay | last post by:
Hi, How I can count certain word in memo field? For example I have 15 words in one field and "PLUMING" word repeats in it 3 times. So I would like my code give me number three as the answer....
2
by: Stephajn Craig | last post by:
Is there a way that I can have a page do some highlighting based on a set of given keywords? The scenario is that a user does a search for certain keywords, and then the items returned have the...
2
by: Stephajn Craig | last post by:
The senario is that a user fills out a form specifying some keywords they want to search a knowledgebase for. For each article that comes back, the words in the article that match their keywords...
7
by: Sling | last post by:
I code in Rexx on the mainframe which has 2 built-in functions: word(s,i) & words(s). word(s,i) returns the ith word in the s(tring), and words(s) returns the number of words within the s(tring)....
6
by: Xernoth | last post by:
Hi, I have an exercise that requests the following: Write a function that reads words from an input stream and stores them in a vector. Use that function both to write programs that count the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.