Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add "" around a variable html tag 1

Status
Not open for further replies.

ThomasJSmart

Programmer
Joined
Sep 16, 2002
Messages
634
bit of a problem here.

im getting text from a database, this text is html formatted and the problem is the text is given to me (posted) in this format:

<font face=verdana color=#FFFFFF> bla </font>

as you can see the &quot;'s are missing,
now for html use this doesnt realy matter, problem is i need the text for flash :( and flash needs the quotes which is needlesly to say quite anoying.

now iv been doing ok in php up to now with this trick:

$string = str_replace(&quot;face=verdana&quot;,&quot;face=\&quot;verdana\&quot;&quot;,$string);

the problem arises with the color tag....

get my drift?
to many options,
color=# is easy, i can str_replace this no problem but the end is causing a difficulty

str_replace (&quot;f&quot;,&quot;f\&quot;&quot;) isnt realy gonna work cus then it will turn freak into f&quot;reak in my regular text.


str_replace (&quot;color=#ffffff&quot;,&quot;color=\&quot;#ffffff\&quot;&quot;) isnt going to work because there are.... how many colors? lots

and now im kinda at a loss ....

any help apreciated, im open to any ideas be they php, flash or javascript based.

i can change the text befor it goes into the database after its been posted and when i get it out of the database, i just cant change the fact that the form is posted without the &quot;'s

thanks,
Thomas



I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
and how do i get the double-quotes in ther?

I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
text is gotten from database and then presented to user
where is the space for me to type?

I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
Sorry. Teach me to skim posts and not read them fully.

This sounds like a job for regular expressions. If you're placing quotes around attributes, then what you want is to look for <string> equalsign <string> space or <string> equalsign <string> closetrianglebracket.

Then replace that with:
dquote <string> dquote equalsign dquote <string> dquote <endcharacter>

Want the best answers? Ask the best questions: TANSTAAFL!!
 
In between the database and the user you can do what ever you want with it.

To err is human, to completely mess up takes a computer. [morning]
 
regular expressions?

i should use preg_replace instead of str_replace?
hmm, i'll have to do some reasearch on that, never used it :)

btr get busy then

thanks,

I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
Oops. My &quot;replace with&quot; string should have read &quot;<string> equalsign dquote <string> dquote <endcharacter>&quot;

Yes, I would use preg_replace().

Here's a code snippet that works with your example HTML code:

Code:
<?php
$foo = '<font face=verdana color=#FFFFFF> bla </font>';

print preg_replace ('/([^ =]+)=([^> ]+)([ >])/', '$1=&quot;$2&quot;$3', $foo);

?>

The only thing is that it doesn't only do the replacement inside an HTML tag. But it shouldn't be too hard to wrap enough code around the snippet to make it work.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
this works:

$text = &quot;<font color=#ffffff size=2 face=verdana> foo </font>&quot;;

$text = preg_replace(&quot;/\<font (.+?)\=(.+?) (.+?)\=(.+?) (.+?)\=(.+?)\>/&quot;, &quot;<font $1=\&quot;$2\&quot; $3=\&quot;$4\&quot; $5=\&quot;$6\&quot;>&quot;, $text);

but it doesnt work if a font tag only has 2 settings like:

<font face=verdana size=6> foo </font>


I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
after looking at the database texts i see that i only need to change font tags by the way,

I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
i just tried to add

$text = preg_replace ('/\([^ =]+)=([^> ]+)([ >])/', '$1=&quot;$2&quot;$3', $text);

to my main script and i got this error:

Warning: Compilation failed: unmatched parentheses at offset 8 in ***/cms.html on line 18


I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
In your regex you have the opening paranthesis esacped with a backslash. That means a 'literal' paranethesis.
Remove the \ backslash and you should be all set.
Code:
$text = preg_replace ('/
\
Code:
([^ =]+)=([^> ]+)([ >])/', '$1=&quot;$2&quot;$3', $text);
 
oepsie. that was my bad i see now..

hehe

thanks to both of you :)

I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top