473,396 Members | 2,010 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,396 software developers and data experts.

strip html tag by replacing comma at the end of each tag

194 100+
Hi everybody,

i have some data which i copied from a web page. its all option values of a select box. now im trying to find a way where i can add a coma at the end of each </option> tag.

here is my code which i took from php.net:
Expand|Select|Wrap|Line Numbers
  1.  
  2. $text = '<option value="test 1">test 1</option><option value="test 2">test 2</option>';
  3.         $text = preg_replace('/</',' <',$text);
  4.         $text = preg_replace('/>,/','> ',$text);
  5.         $desc = html_entity_decode(strip_tags($text));
  6.         $desc = preg_replace('/[\n\r\t]/',' ',$desc);
  7.         $desc = preg_replace('/  /',' ',$desc);
  8.         echo preg_replace('/[\ ]/', ',', $desc);
  9.  
  10.  
this is replacing each empty space with a coma not at the end of tag.
please anybody can help me to find out a way where i can do it?
Jul 14 '10 #1

✓ answered by zorgi

Oups sorry you can just add string rtrim ( string $str [, string $charlist ] ) to fix the problem of the last comma:

Expand|Select|Wrap|Line Numbers
  1. echo rtrim(str_replace("</option>", "</option>,", $text), ",");
  2.  

4 1945
zorgi
431 Expert 256MB
You don't need all that. You can just use str_replace.
This will add comma on the end of each </option> tag:

Expand|Select|Wrap|Line Numbers
  1. echo str_replace("</option>", "</option>,", $text);
  2.  
But what I don't understand is what do you need something like that for?
Jul 14 '10 #2
mfaisalwarraich
194 100+
it worked thanx. well i need it to have a coma separated values and then im gona explode that values and then insert them into database using foreach loop. its adding a coma at the end of text too but i think i can check for a value if its empty before inserting into the database.
Jul 14 '10 #3
zorgi
431 Expert 256MB
Oups sorry you can just add string rtrim ( string $str [, string $charlist ] ) to fix the problem of the last comma:

Expand|Select|Wrap|Line Numbers
  1. echo rtrim(str_replace("</option>", "</option>,", $text), ",");
  2.  
Jul 14 '10 #4
mfaisalwarraich
194 100+
thank you again for ur help.
Jul 14 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Fazer | last post by:
Hello, I was wondering what would be the easiest way to strip away HTML tags from a string? Or how would I remove everything between < and > also the < , > as well using regex? Thanks for...
2
by: M.L. | last post by:
I'd like to know if a javascript technology exists that, given a set of HTML files, would allow me extract cell data from a row in an Excel or csv file and place it into specified variables within...
9
by: Glenn | last post by:
Is it possible to only redraw a portion of an HTML page as part of a request? With each request, I am updating number of results but do not want to lose where you were in a page. I created...
2
by: AdamD | last post by:
Does anyone have an example of how to strip HTML tags from an laready existing text file Thanks
3
by: Michal A. Valasek | last post by:
Hello, I want to transform text with HTML markup to plain text. Is there some simple way how to do it? I can surely write my own function, which would simply strip everything with < and >....
1
by: KB | last post by:
My fellow virtual friends, allow me to ask for your collaboration with this simple yet not-so-trivial matter, My application has a menu which is defined and stored in a database. This menu will...
6
by: Shelby | last post by:
Hi, is there any StripHTMLTag function in VB.Net? i would like to remove all html tag in a string. Thanks.
2
by: tshad | last post by:
Is there an easy way to strip HTML tags from Text to get just the plain text? I am using a program called FreeTextBox that lets you format Text in a TextBox. It does this by adding HTML tags...
7
by: KiwiGenie | last post by:
I have a form which lists selected records, there could be anything from 1 record to all records selected. I want to output a report for each record as HTML. I want each file to take its name from a...
5
by: dgriff80 | last post by:
hello all! I am using a form that a user can fill out but for security reasons I want html stripped out. If the user inputs html, I want it to kick back saying something to the fact that it had...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.