splitting a string by character skipping html tags pls read
I need some help and advice with something
I am trying to do the following
User inputs some information into a text area this info is a few paragraphs of text with possible html tags
I want to have them submit this and for php to take all the non html portions of the text they submit and split it by each individual character and insert something inbetween each character but fotr it not to do this with any of the html tags
so a sentance "some random sentance" would be split and added such as "s-o-m-e- -r-a-n-d-o-m- -s-e-n-t-a-n-c-e-"
But if if contained html tags it should be like this
"some <br> random <br> <a href="somewhere.html">sentance</a>" would be split and added such as "s-o-m-e- <br> -r-a-n-d-o-m- <br> <a href="somewhere.html">-s-e-n-t-a-n-c-e-</a>"
However with the current script im using it messes up the html tags to be like this
"some <br> random <br> <a href="somewhere.html">sentance</a>" would be split and added such as "s-o-m-e- -<-b-r->- -r-a-n-d-o-m- -<-b-r->- -<-a- -h-r-e-f-=-"-s-o-m-e-w-h-e-r-e-.-h-t-m-l-"->-s-e-n-t-a-n-c-e-<-/-a->-"
I am currently using the followinf php5 code which works fine on just plain text but has no way of skipping html tags, it generates a text area containing a modified version of the text inputted witha - between every character but i need it to skip characters that are part of a html tag and keep the tags fully intact and working as they should
PHP Code:
if($_POST['enc']==1){
$text = $_POST['text'];
$encode1=utf8_encode($text);//first encode where $string is the supplied string
$l=strlen($encode1);
$a='';
for($i; $i<$l; $i++)
{
$s=substr($encode1, $i, 1);//get character
$plusone= $s;
$a.=$plusone.'-';
}
$encode2=utf8_encode($b);//encode second time
echo '<center><textarea cols="80" rows="10">'.$encode2.'</textarea></center>';
}
Any ideas or suggestions on where to start or what to look into or sample code etc would be most appreciated
Thanks in advance
OriginalFundRaisingWidget - Free "Non Commercial" PayPal FundRaising Widget, for Charities, Organizations and Individuals, Hosted Free. Check It Out
Say if i wanted to do that but instead of seperating with a - i wanted to use a randomly generated character or multiple characters
I have the script that can do the random stuff just wasnt sure how i would get your code to input the random character instead of the - and to make each thing random so not the same random inbetween each character in the text but a different one generated each time
OriginalFundRaisingWidget - Free "Non Commercial" PayPal FundRaising Widget, for Charities, Organizations and Individuals, Hosted Free. Check It Out
The first one worked only if $text was enclosed between tags (it started with an opening-tag and ended with a closing-tag). This one works anyway.
Good luck
Edit: Sorry, I didn't see your post when I posted this one. To change the dash, just change the second parameter passed to preg_replace with "$1someCharacter(s)", so all characters will be followed by someCharacter(s).
ok yours is working now thanks, ok ive figured out that if i make it so the bit thats added in between each character is some unique reference such as <<Random>> i should some how be able to go through and replace that with some random generated characters but im wondering if i take the final result from your script and it contains that unique reference between each character that isnt a html tag
How would go through and replace each unique tag with a new randomly generated character
i would need to some how get the result into an array that i could pass through and each unique tag would get replaced by generating a new random character each time until all the tags ahad been replaced
But im unsure how i woudl add that little bit in after your code
OriginalFundRaisingWidget - Free "Non Commercial" PayPal FundRaising Widget, for Charities, Organizations and Individuals, Hosted Free. Check It Out
That is very strange, since it works perfectly for me. Try copy-pasting again..
Here's what it outputs for "<p>Heeee<b>eeell</b>oooo!</p>" on my machine:
Code:
<p>H!e#e(e@e^<b>e-e=e@l-l(</b>o=o-o$o(!#</p>
I'm sorry but I can not help you any further since this works on my PC
hi bharanikumarphp,
I think we (i and znupi) are not counting the character.
We are using regular expression / regex (easy way to say : pattern match).
If the pattern : match, than replace it with something.
In your case :
if i am not misunderstanding your problem, you want to separate each word with separator.
I think this is easier than the original problem.
Forget the code above, this more simple :
This case consider the original word separator is space (" "), in html is .
The first way is :
You can use explode() and implode() function to separate each word into array, and connect them again with the separator.
Weakness in this way is : if the original separator is double (triple and so on) space (" ") or in html entities ( , it will not recognize it. Or if the input is just spaces...
Second way do it better. It use regex.
If you want to replace the separator with random char, just replace 45 with random function to pick number between and choose ascii.
For example : rand(33,64).
Hope this help you...
We do more than just count...
Last edited by andre4s_y; 07-04-2008 at 09:02 AM.
Reason: forget to output something :P
The below is my content,
1.I want to put the <--break--> after 1075 character has reached,
2.Never broke the html tag,exampl()<<--break-->table>
3.And also if the cursor meet the html tag then put the <--break--> after the html tag has closed,example()<table><tr><td>this is test</td></tr></table><--break-->
PHP Code:
<I>Editor?s note: Laser-deposition
welding is an important alternative to more conventional mold-repair techniques. It
is beginning to find acceptance in U.S. mold shops, having previously gained a
foothold in Europe. <!--author_start-->Richard Hendel<!--author_end-->,
product manager for Rofin-Sinar, describes what laser-deposition welding is and
how it works.</I> <br> <br><TABLE ALIGN="right" BORDER=0
CELLSPACING=0 CELLPADDING=10 width="299"> <TR> <TD><img
src="/images/2002/March/Tooling_RofinSW-Performance.jpg" width="279"
height="297"></TD> </TR> <TR> <TD><font size="1" face="Arial, Helvetica,
sans-serif"><!--abstract_start-->The StarWeld laser welding machine is used to
spot and seam weld high-grade steel alloys, copper, gold, silver, platinum, and
titanium, in a variety of combinations. Output power ranges from 20 to
500W.<!--abstract_end--> </font></TD> </TR> </TABLE>Laser deposition
welding technology is beginning to find its place in modification and repair of
molds. A typical application would be the repair of an injection mold constructed of
cold work steel, which is subject to heavy wear on the edges caused by
processing of glass-fiber-reinforced material. The chipped or rounded edge areas
can be laser-deposit welded to fill cracks, using a wire diameter of .4 mm. After
repair, the insert and the mold have a service life at least equal to that of the
original compon
Bookmarks