www.webdeveloper.com
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 46

Thread: splitting a string by character skipping html tags pls read

  1. #1
    Join Date
    Sep 2006
    Location
    Wixom, MI
    Posts
    292

    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$i1);//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

  2. #2
    Join Date
    Sep 2006
    Location
    Bucharest, RO
    Posts
    940
    Here's a way to do it:
    PHP Code:

    $result 
    =  preg_replace("%([^>])(?=[^>]*<)%""$1-"$text);
    echo 
    $result
    Hope that helps

  3. #3
    Join Date
    Sep 2006
    Location
    Wixom, MI
    Posts
    292
    Looks good

    One more question

    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

  4. #4
    Join Date
    Sep 2006
    Location
    Bucharest, RO
    Posts
    940
    Actually, here's a better way:
    PHP Code:
    $result preg_replace("%(?<=[^<])([^<>])(?=[^>])%""$1-"$text);
    echo 
    $result
    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).
    Last edited by Znupi; 12-10-2007 at 02:40 PM.

  5. #5
    Join Date
    Jun 2007
    Location
    Tangerang, Banten
    Posts
    258
    Maybe this code bigger..
    but maybe can help you..
    PHP Code:
    <?php
        
    function add_minus($t)
        {
            
    $t preg_replace("/(\w)/","-$1-",$t);
            
    $t preg_replace("/\-\-/","-",$t);
            return 
    $t;
        }
        
    $text "some <br> random <br> <a href=\"somewhere.html\" onclick=\"alert('test')\">sentance</a>";
        
    $temp explode(" ",$text);
        for(
    $i=0$i<count($temp); $i++)
        {
            if(
    preg_match("/\<?(.*)\>/",$temp[$i]))
            {
                if(
    preg_match("/\>(.*)\</",$temp[$i],$between))
                {
                    
    $between add_minus($between[1]);
                    
    $temp[$i] = preg_replace("/\>(.*)\</",">$between<",$temp[$i]);
                }
            }
            elseif(
    preg_match("/\<(.*)\>?/",$temp[$i]))
            {}
            elseif(
    preg_match("/.+\=\".*\"/",$temp[$i]))
            {}
            else 
            {
                
    $temp[$i] = add_minus($temp[$i]);
            }
        }
        
    $result implode(" ",$temp);
        echo 
    htmlentities($result);
    ?>
    input :
    some <br> random <br> <a href="somewhere.html" onclick="alert('test')">sentance</a>
    output :
    -s-o-m-e- <br> -r-a-n-d-o-m- <br> <a href="somewhere.html" onclick="alert('test')">-s-e-n-t-a-n-c-e-</a>
    Last edited by andre4s_y; 12-10-2007 at 03:22 PM.

  6. #6
    Join Date
    Sep 2006
    Location
    Wixom, MI
    Posts
    292
    andre4s y

    With your code i get the following error

    Warning: preg_replace() [function.preg-replace]: Compilation failed: lookbehind assertion is not fixed length at offset 8
    for this line of code

    Code:
    $result = preg_replace("%(?<=[^<])([^<>])(?=[^>])%", "$1-", $text);

  7. #7
    Join Date
    Jun 2007
    Location
    Tangerang, Banten
    Posts
    258
    Sorry... i have editted it...
    I was trying Znupi code before...

  8. #8
    Join Date
    Sep 2006
    Location
    Wixom, MI
    Posts
    292
    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

  9. #9
    Join Date
    Sep 2006
    Location
    Bucharest, RO
    Posts
    940
    Here you go:
    PHP Code:
    $x preg_replace("%(?<=[^<])([^<>])(?=[^>])%""$1" chr(0), $text);

    $a "=-*@^$#!()";
    while (
    strpos($xchr(0))!==false) {
        
    $p strpos($xchr(0));
        
    $x[$p] = $a[mt_rand(0strlen($a)-1)];
    }

    echo 
    $x
    Where $a is a string containing all the characters you want randomly placed between characters in $text.
    Have fun

  10. #10
    Join Date
    Sep 2006
    Location
    Wixom, MI
    Posts
    292
    Znupi

    tried your code and i get that error mentioned above

  11. #11
    Join Date
    Jun 2007
    Location
    Tangerang, Banten
    Posts
    258
    BWWebDesigns,
    actually i do not fully understand what is your purpose...
    But, this code maybe not far away with your purpose..
    PHP Code:
    <?php 
        
    function add_tag($t
        {     
    //add unique tag
            
    $t preg_replace("/(.)/","[r]$1[r]",$t); 
            
    $t preg_replace("/(\[r\]){2}/","[r]",$t); 
            return 
    $t
        } 
        
    $text "some <br> random <br> <a href=\"somewhere.html\" onclick=\"alert('test')\">sentance</a>"
        
    $temp explode(" ",$text); 
        for(
    $i=0$i<count($temp); $i++) 
        { 
            if(
    preg_match("/\<?\w(.*)\>/",$temp[$i])) 
            { 
                if(
    preg_match("/\>(.*)\</",$temp[$i],$between)) 
                { 
                    
    $between add_tag($between[1]); 
                    
    $temp[$i] = preg_replace("/\>(.*)\</",">$between<",$temp[$i]); 
                } 
                elseif(
    preg_match("/[\<|\>]{2}/",$temp[$i],$between))
                {
                    
    $temp[$i] = add_tag($temp[$i]); 
                }
            } 
            elseif(
    preg_match("/\<(.*)\>?/",$temp[$i])) 
            {} 
            elseif(
    preg_match("/.+\=\".*\"/",$temp[$i])) 
            {} 
            else 
            { 
                
    $temp[$i] = add_tag($temp[$i]); 
            } 
        } 
        
    $result implode(" ",$temp); 
        echo 
    htmlentities($result)."<br />"
        
    $result preg_replace("/\[r\]/e","chr(rand(33,64));",$result); //replace tag with random char
        
    echo htmlentities($result); 
    ?>
    The output in my computer is :
    [r]s[r]o[r]m[r]e[r] <br> [r]r[r]a[r]n[r]d[r]o[r]m[r] <br> <a href="somewhere.html" onclick="alert('test')">[r]s[r]e[r]n[r]t[r]a[r]n[r]c[r]e[r]</a>
    0s<o"m'e@ <br> .r@a%n"d0o?m, <br> <a href="somewhere.html" onclick="alert('test')">6s/e9n4t%a*n#c@e=</a>
    Hope that help you.....

  12. #12
    Join Date
    Sep 2006
    Location
    Bucharest, RO
    Posts
    940
    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

  13. #13
    Join Date
    Jul 2008
    Posts
    63

    character count is use full

    Hi dear i have used ur code, forputing the special character between,2character,


    its very use full,,

    but can u tell me, where u are counting the character,

    Can u tell me,which place

    u r code put the special character after 1 character only..

    u-h-h-f-r-h

    but am expecting

    ganesh-srinivasan-john

    like this

    can u tell idea

  14. #14
    Join Date
    Jun 2007
    Location
    Tangerang, Banten
    Posts
    258
    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 :
    PHP Code:
    <?php
    $text_ori 
    "ganesh srinivasan john";
    //first way
    $text explode(" ",$text_ori);
    $text implode(chr(45),$text);
    echo 
    $text;
    echo 
    "<br />";
    //second way
    $text preg_replace("/\b(\s+|&nbsp;)\b/",chr(45),$text_ori);
    echo 
    $text;
    ?>
    the output is :
    ganesh-srinivasan-john
    ganesh-srinivasan-john
    This case consider the original word separator is space (" "), in html is &nbsp;.
    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 (&nbsp, 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 10:02 AM. Reason: forget to output something :P

  15. #15
    Join Date
    Jul 2008
    Posts
    63

    see dear

    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 noteLaser-deposition 
    welding is an important alternative to more conventional mold
    -repair techniquesIt 
    is beginning to find acceptance in U
    .Smold shopshaving previously gained a 
    foothold in Europe
    . <!--author_start-->Richard Hendel<!--author_end-->, 
    product manager for Rofin-Sinardescribes what laser-deposition welding is and 
    how it works.</I> <br> <br><TABLE ALIGN="right" BORDER=
    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 alloyscoppergoldsilverplatinum, and 
    titaniumin a variety of combinationsOutput 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 materialThe chipped or rounded edge areas 
    can be laser
    -deposit welded to fill cracksusing a wire diameter of .4 mmAfter 
    repair
    the insert and the mold have a service life at least equal to that of the 
    original compon 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles