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,
now it's clear!
I think I could do the job by a &quot;scanning by character&quot; system (rhe code will not certainly win a &quot;nobel for elegance&quot; and it would be quite old fashiioned, but sometimes...).
If I don't hear from you untill sunday, I will have a travel by train on monday and I will be glad to avoid the bother of travel solving your problem on my notebook (unless a rainy sunday will constraine me at home, near a PC...)!
Gaetano
 
casters, lol :)
a scan char-by-char would be wonderfull, it could run on the $HTMlc (to-be-replaced array) and transform the chars into their respective <img> tags!
oh, and old fashioned code is sometimes much better than modern high.teched codes ;)

about the train, lol, thanks, u'll avoid getting bored solving a problem that'll help others (me :p). that's great :) hope it doesn't rain btw :p
so, have a good sunday and monday on that train :p

ehe, tia !
 
Hmm... I'm doing quite similar things in my word wrapper. I found that the easiest way to do it was to loop through all the characters.

//Daniel
 
sorry for the last post, it's &quot;
danielhozac, and how do u do that ? :)
&quot;
tia :)
 
After having cleaned the code to only include the HTML ignoring parts, and changed it to fit your purposes a bit better, this is what I came up with
Code:
$string1 = &quot;a string <b>with bold text</b> and <i>italics</i>&quot;;
$string2 = &quot;&quot;;
$intag = false;
$length = strlen($string1);
for ($i = 0; $i < $length; $i++)
{
	if ($string1[$i] == '<' && $string1[$i + 1] != ' ')
		$intag = true;
	elseif ($string1[$i] == '>' && $intag == true)
		$intag = false;
	elseif ($intag == false)
		$string2 .= '<img src=&quot;' . $string1[$i] . '.jpg&quot; />';
}
Note that it still has problems with strings like &quot;I think 3<5&quot;.

//Daniel
 
Whoops... If you want the tags to be there, it would be
Code:
$string1 = &quot;a string <b>with bold text</b> and <i>italics</i>&quot;;
$string2 = &quot;&quot;;
$intag = false;
$length = strlen($string1);
for ($i = 0; $i < $length; $i++)
{
	if ($string1[$i] == '<' && $string1[$i + 1] != ' ')
		$intag = true, $string2 .= '<';
	elseif ($string1[$i] == '>' && $intag == true)
		$intag = false, $string2 .= '>';
	elseif ($intag == false)
		$string2 .= '<img src=&quot;' . $string1[$i] . '.jpg&quot; />';
	else
		$string2 .= $string1[$i];
}

//Daniel
 
Dear James, sadly I was at home this morning. Now I see that Daniel gave you a solution very similar
to the one I prepared. Nevertheless, I think that my solution has some more funtionalities, so...here it is:

<?PHP
$string_in = &quot;test string with some<b>tag, some space and *</b>&quot;;
$string_out = &quot;&quot;;
$tag_flag = 0;
$all_in_one=1; //if you have very big text, than you could prefer to output the string while processing $all_in_one=0)
$ante_txt=&quot;<img src=&quot;;
$font_dir=&quot;arial/&quot;; //you can put images of various fonts in different directories and choise among them!
$post_txt=&quot;.gif>&quot;;
$tot_len = strlen($string_in);
for ($cur_let = 0; $cur_let < $tot_len; $cur_let++)
{
//Echo &quot;$string_in[$cur_let]&quot;;
if ($string_in[$cur_let] == '>' and $tag_flag == 1)
{
$tag_flag=0 ;
if ($all_in_one==1)
$string_out= $string_out . '>';
else
echo &quot;>&quot;;
}
elseif ($tag_flag == 1)
{
if ($all_in_one==1)
$string_out= $string_out . $string_in[$cur_let];
else
echo $string_in[$cur_let];
}
elseif ($string_in[$cur_let] == '<' and $tag_flag==0)
{
$tag_flag = 1;
if ($all_in_one=1)
$string_out= $string_out . '<';
else
echo &quot;<&quot;;
}
else
{
switch($string_in[$cur_let]) //you need this cases to cope with letter that cannot be filename ( ,*,+-?...)
{
case &quot; &quot;:
if ($all_in_one==1)
$string_out= $string_out . $ante_txt . $font_dir . &quot;blank&quot; . $post_txt;
else
echo $ante_txt . $font_dir . &quot;blank&quot; . $post_txt;
break;

case &quot;,&quot;:
if ($all_in_one==1)
$string_out= $string_out . $ante_txt . $font_dir . &quot;comma&quot; . $post_txt;
else
echo $ante_txt . $font_dir . &quot;comma&quot; . $post_txt;
break;

case &quot;?&quot;:
if ($all_in_one==1)
$string_out= $string_out . $ante_txt . $font_dir . &quot;quest_mark&quot; . $post_txt;
else
echo $ante_txt . $font_dir . &quot;quest_mark&quot; . $post_txt;
break;

case &quot;*&quot;:
if ($all_in_one==1)
$string_out= $string_out . $ante_txt . $font_dir . &quot;asterisk&quot; . $post_txt;
else
echo $ante_txt . $font_dir . &quot;asterisk&quot; . $post_txt;
break;

default:
if ($all_in_one==1)
$string_out= $string_out . $ante_txt . $font_dir . $string_in[$cur_let] . $post_txt;
else
echo $ante_txt . $font_dir . $string_in[$cur_let] . $post_txt;
}
}
}
if ($all_in_one==1)
echo &quot;$string_out&quot;;
?>

If something it not clear, let me know.
I hope it works because I had no time to test it enough!
A last advise: if you think that in your string there could be some special characters (&nbsp;,for example) than you should replace them fefore running the program.

Gaetano
 
James,
I see that in the last advise, the example in brackets has been parsed and it is not understandable: it is related to the special characters beginning with the ampersand symbol.
Gaetano
 
guys thank u so much, both of your procedures worked perfectly ! u sure earned your stars !
thank u all for your help, and casters thanks for your time building that fine procedure ! it sure worked perfectly :p
oh, and good look for your train trip tomorrow.. u should have thought of my problem tomorrow, now you'll get bored ! :|
lol, stay well all of you!

[[]]
 
James, I'm glad to know it really works and I hope you'll find useful the potentialities I have added to the procedure. Of course you can optimize the code and transform it in a function, for better use.
I think that sooner or later I will use this routine, because it's an interesting idea, to be able to generate texts with particular fonts &quot;on the flight&quot;, without preparing all the relevant images.
As to the travel...I'll manage with some non-informatical magazines!

Regards
Gaetano
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top