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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string replacement (advanced.. :\) 4

Status
Not open for further replies.

jamesp0tter

Programmer
Feb 20, 2003
119
0
0
PT
well, i asked for help some days ago regarding string replacement in thread434-520275 . the solution provived was a very good work-around to what i was working in.. i needed to replace smilies only if they were not inside html tags, so i'm replacing them for " :) " and no ":)". that'll do it for now.


BUT

i started a new project, and i now have the same problem, but in a little more complicated form:
i need to replace ALL TEXT that is not inside HTML tags &quot;< >&quot;

for example, if i replace all &quot;a&quot;'s for &quot;b&quot;'s , i would have <b> tag insted of an <a> ones.
like, i've got &quot;<a href=\&quot;someplace.php\&quot;>aaaa</a>&quot; i need to replace the &quot;aaaa&quot; only, not the ones that are inside the &quot;< >&quot; tags.

any ideas are much apreciated!
Thx in advance !
 
James,
I think what you are looking for are &quot;regular expression&quot;, like eregi_replace(). They are very powerful, though quite complex. Try and look on a manual.
Gaetano
 
James, it's me again.
Looking carefully at your problem, I'm not so sure that it can be easy solved just with regular expressions. I hope there is a better away that scanning all the character, deciding what to do on the base of the adjacent characters, Monday I hope I'll have sometime to dedicate to this (if the problem will not be already solved by sleipnir214.....)
Casters
 
casters, first of all thank you for your help

i got into ereg_replace manual section in php.net and that definetly is very complicated.. hmm, i got some experience with preg_replace, but ereg_replace seems completely different.

btw, the example i gave may make it seem like i need to replace one character to another.. that's not actually what i need, but someting to replace a character to a html tag, like:

&quot;a&quot; -> &quot;<img src=\&quot;a.gif\&quot;>

hope this doesn't complicate things even more, as it seems that this is a very complex problem :
i was thinking of something alike:
preg_replace(&quot;'<(.*?>(.*?)</(.*?)>'si&quot;, &quot;someway to replace each char in \$1 with html tag&quot;, $str);

maybe not huh ? :
thank u very much for your help casters, and anyone else that's trying to help me out here.. !
 
Well, now I've understood!
I fear that the only solution would be to scan all the string with a routine taht checks character by character if you are inside the tags, replacing the desidered characters only if you are outside the tags.
If yuo don't find any other solution AND you REALLY need to follow this procedure AND you don't know how to do it, let me know and I'll try to help you.
In the meantime, have a good week-end!

Gaetano
 
Will it always be the same change, i.e. - you will always take a character not in html tags and turn it into a <img src=\&quot;a\&quot;>?

also

will the string be the same consitency, i.e.- you will always look at a string the is made up of <something>needschanged</something>? or will it be more complex?

Reid Givens
Webdeveloper
 
casters, yes, i think that's it, checking all chars to see if they're not inside < > and replace them. i really can't see how, so please help :)
if i find a solution meanwhile, i'll write it here so u don't waste more time !
and a good weekend to you too !

reidgivens, yes, i basically want to turn every char into a correspondent <img>, and yes, HTML doesn't get more complex than that :) if i have scripts like java that i don't want to convert, i'll make sure they don't get parsed as well, like including them from another page.

thank u so much for your help guys, hope to hear from you soon !
 
preg_replace is what you're going to want, ereg is the same idea, just using a different syntax for the regular expressions.

preg expressions are perl compatabile.

so, you need to construct the pattern...

I'm sorry, I'm a little fuzzy, I know you've expressed what you want to do, but, given that regular expressions are just finite state machines, having a very clear definition of the problem is key in writing the expression.

So, let me understand the problem completely before thinking too much harder,

anytime you encounter a character on it's own, you want to replace it with an image tag where the target is a gif who's filename is the same as that single character?

-Rob
 
if this is the case, it may not be the best option, but you could try to explode the string into an array using the > as the separating character, giving you this

<a href=\&quot;someplace.php\&quot;>aaaa</a>

turns into

$array[0]=<a href=\&quot;someplace.php\&quot;
$array[1]=aaaa</a

Then take out the </a with a str_replace, to give you just the type in between

then change it to what you want and reassemble the string

This is a sort of sloppy way of doing it, but if you are in a huge rush, it migh suffice for now.

I'm interested in what casters might say about this, let me know Reid Givens
Webdeveloper
 
skiflyer, that's right, replace all chars with a <img src=&quot;$CHAR$.gif&quot;>
i got &quot;some&quot; experience with preg_replace, i thought of something like

preg_replace(&quot;'<(.*?)>(.*?)'si&quot;, &quot;ignore \$1, it's inside < >, and do what i want with \$2&quot; , $str);

well, i would have like 50 chars in $2, i don't know how to separate each and assign them a <img> tag.

reidgivens, that's a pretty good idea! i'll work that out and post here what i come out with! tks
 
You ever just know something isn't that hard, but yet it's friday afternoon...

Code:
$string = &quot;la de <b>da</b> my friend&quot;;
$string2 = preg_replace(&quot;/([ ][^\<][^ ]+)\b/&quot;, &quot;<i>\$1</i>&quot;, $string);
echo $string2;

is deceptively close, yet quite far off. I can't seem to get away from the problems of
a) if I replace the first space with a \b, it goofs up, so the first word on the line is always being ignored, in this case, la.

Code:
b) I'm seriously oversimplifying the idea of an html tag to anything that starts with a < and has no spaces in it until the end of the word, so tags like
<b>work</b> just fine, but a tag like
<b>will no</b> work at all

this is a minor issue compared to the former I do believe... anyway, they expect me to do some work at work, so perhaps later I'll look again, or tonight when I get home. The only issue left here, is I think you may be oversimplifying the idea of an HTML tag in your definition, but maybe that's ok by definition... for example
<input type=&quot;button&quot; value=&quot;<<&quot; name=&quot;previouspage&quot;>
<input type=&quot;button&quot; value=&quot;>>&quot; name=&quot;nextpage&quot;>

and both could be written as

<input type=&quot;button&quot; value=&quot;<<&quot; name=&quot;previouspage&quot; />
<input type=&quot;button&quot; value=&quot;>>&quot; name=&quot;nextpage&quot; />

and still be quite legal, both options being more interesting to match than
<b>Hello World</b>

Which leads to my question of, in
<b>H I</b>
You want,
<b><img src=&quot;H.gif&quot;> <img src=&quot;I.gif&quot;></b>
or you want it ignored because it's within a tag?

What I'm really thinking here is it's time to tokenize, that would make this so much simpler... but as I started to say I'm sposed to be working, so more over the weekend from me on this topic.

Hope it might point you somewhere in the meantime.

-Rob
 
ok, i'm closer to finding out the solution to this :)
i used:

<?

$HTML = &quot;some tstringggg <html stuff> bla bla <yeh> hehe&quot;;
$HTML = explode(&quot;>&quot;, $HTML);
$i = 0;
while ($i <= count($HTML)) {
$HTMl = preg_replace(&quot;'<(.*)'&quot;, &quot;&quot;, $HTML[$i]);
echo &quot;Array($i): $HTMl \n\n&quot;;
$i++;
}

?>

ok, i got all the text to replace in their arrays, waiting to be replaced.

BUT

what about the tags i exploded/deleted up there ? i need them also, like <img> tags and <a> tags in the midle of the text.. :\
 
skiflyer, that worked.. but that first word not showing and the other issues.. erm :about that &quot;<<&quot; names in forms don't worry, i won't put stuff like that in the page :)
abou that <b>H I</b>, yes, i want to keep what's inside < > like it is and replace what's outside it !
 
My mind is just not functioning this afternoon, but maybe this little bit will help, it does the opposite of what you want...

Code:
$string = &quot;la de <b>da</b> my friend&quot;;
$tag = &quot;[\<]([^\>])*[\>]&quot;;
$string2 = preg_replace(&quot;/($tag)/&quot;, &quot;TAG$1ENDTAG&quot;, $string);

It will surround all tags with the words TAG and ENDTAG, now flipping it arround to do the opposite should be trivial, but I gots managers right behind my shoulders, so until I get some time, hopefully that'll help you go the right direction, or allow someone else to whip up the answer.

-Rob
 
ok guys, i found the solution.
thank u so much for your help, i'm going to clean up the code and then i'll post it here for everyone who may want to use it!
oh, and obviously a star for each of you for your help!

bb in a sec
 
Hi, James
I was casually passing by a PC and I've read (quite quickly, to say the truth!) the continue of the thread. I thought to something similar to the last proposal of skiflier, but...I'm not sure it could work, most of all because...
could you please provide two or three examples of phrases that you need to process and how exactly the processed output would be? It's not still entirely clear to me how your text is structured and which tags can be found, where, and so on). I did not understand if you only (and always) have text to be processed included between tags, if you have other tags in the text that don't embed text to be processed.
In the meantime...good luck!
Gaetano
I'll
 
ok people, i got a bit excited for something i thought would work, but it doesn't..

it was something based in that explode by &quot;>&quot;, but then i have an <img> tag & i don't know how to keep that from being replaced..

any other ideas ? :| maybe those ereg_replace's & preg_replace's you where thinking of ?

tks
 
James,
maybe you &quot;skipeed&quot; my last message.
Please provide us some exhaustive example.

By
Gaetano
 
casters, i'm terribly sorry for that :| i missed it

well, here are some examples:
<b>hello ! </b> welcome to <i>my</i> <img src=&quot;site.gif&quot;> SITE ! <br>

would become:
<b><img src=&quot;h.gif&quot;><img src=&quot;e.gif&quot;><img src=&quot;l.gif&quot;><img src=&quot;l.gif&quot;><img src=&quot;o.gif&quot;> <img src=&quot;!.gif&quot;> </b> <img src=&quot;w.gif&quot;><img src=&quot;e.gif&quot;><img src=&quot;l.gif&quot;><img src=&quot;c.gif&quot;><img src=&quot;o.gif&quot;><img src=&quot;m.gif&quot;><img src=&quot;e.gif&quot;> <img src=&quot;t.gif&quot;><img src=&quot;o.gif&quot;> <i><img src=&quot;m.gif&quot;><img src=&quot;y.gif&quot;></i> <img src=&quot;site.gif&quot;> <img src=&quot;S.gif&quot;><img src=&quot;I.gif&quot;><img src=&quot;T.gif&quot;><img src=&quot;E.gif&quot;> <img src=&quot;!.gif&quot;> <br>

btw, i'll be running this on a UNIX box, so upper and lower are easily distincted (&quot;a.gif&quot; & &quot;A.gif&quot;)

basically, ALL TEXT inside tags REMAINS UNCHANGED, and the rest (ouside < >) gets converted, each character is converted to <img src=&quot;!char!.gif&quot;>.

no special tags will be needed to keep text from being processed.

other example:
<td><tr>cell</tr><tr>CELL</tr></td><br><center>welcome</center><br>

becomes

<td><tr><img src=&quot;c.gif&quot;><img src=&quot;e.gif&quot;><img src=&quot;l.gif&quot;><img src=&quot;l.gif&quot;></tr><tr><img src=&quot;C.gif&quot;><img src=&quot;E.gif&quot;><img src=&quot;L.gif&quot;><img src=&quot;L.gif&quot;></tr></td><br><center><img src=&quot;w.gif&quot;><img src=&quot;e.gif&quot;><img src=&quot;l.gif&quot;><img src=&quot;c.gif&quot;><img src=&quot;o.gif&quot;><img src=&quot;m.gif&quot;><img src=&quot;e.gif&quot;></center><br>

if u need more examples please let me know!
tks!
 
btw, if anyone wants to take this from where i left it, i gave up when i was going to the essencial part, replacing every char for <img>.
i created this based on reidgivens post:

<?

$HTML = &quot;<welcome> hello ! <b> i hope to </b> get this workinggggg <i> and </i> i love php <img lalala> hwhwhw&quot;;

$chars = explode(&quot;>&quot;, $HTML);
$tags = explode(&quot;<&quot;, $HTML);

// ok, life's easy now.. we got an array with text and another with the tags.. next chapter: replacing text (????)

$i = 1;
// i didn't quite understood why first and last arrays of the 'arrays' are blank.. :consuded: , so, we start with 1 and finish with -1:
while ($i <= count($chars)-1) {
$HTMlc = preg_replace(&quot;'<(.*)'&quot;, &quot;&quot;, $chars[$i]); // delete what we don't want
$HTMlt = preg_replace(&quot;'>(.*)'&quot;, &quot;&quot;, $tags[$i]); // delete what we don't want

echo &quot; <$HTMlt> $HTMlc \n\n&quot;; // assuming $HTML starts with a tag, we sart by echoing the tag & then the rest; \n\n is to make it easier for me to understand what's what.

$i++;
}



?>

i come out with:

replace.php
------------
<welcome> hello !

<b> i reallyyyyy

</b> like what u sayd! behave

<i> and be

</i> respected

<img lalala> hwhwhw
--/----------

ok, the only thing we need now is.... the essencial, the replacing routine.. :if anyone knows how to replace each and every char in $HTMlc with <img> respective, and THEN NOT TO REPLACE THE <img> tags placed (here's where i gave up :rolleyes: :\), i think we got the problem solved :)

tia & tks for your support!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top