1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how can i link the words in a text on a query?

Discussion in 'PHP' started by baris22, Oct 18, 2008.

  1. #1
    hello,

    i am displaying a row from mysql. i want to give a search link to every single word on that text with the name of the word. i tried different ways but it did not work.

    This is from the row:

    this is a test

    I want it to be like this:

    this is a test

    any idea?



    thanks
     
    baris22, Oct 18, 2008 IP
  2. Bind

    Bind Peon

    Messages:
    70
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    function CreateLinks($string)
       {
          $linktext = explode(" ",$string);
          foreach ($linktext as $this_linktext)
             {
                echo "<a href=\"search.php?q=$this_linktext\">$this_linktext</a> ";
             }
       }
    
    PHP:
     
    Bind, Oct 18, 2008 IP
    baris22 likes this.
  3. elias_sorensen

    elias_sorensen Active Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Just made this one quick for you... It's untested, but should work.
     
    elias_sorensen, Oct 18, 2008 IP
    baris22 likes this.
  4. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4
    I do not really know how to use function. Thank you for your reply.

    Thank you. it worked. it was what i wanted.
     
    baris22, Oct 18, 2008 IP
  5. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Since you used regex at the beginning, you should continue using it to finish the job
    
    $content = 'this is a test';
    $replaceLink  = 'http://search.php/?q=';
    $newContent = preg_replace("/([\w]{1,})/","<a href=\"$replaceLink$1\">$1</a>", $content);
    print $newContent;
    
    PHP:
    or you need it as a function so that it will be easier to use
    function makeLink($content) {
    $replaceLink  = 'http://search.php/?q=';
    $newContent = preg_replace("/([\w]{1,})/","<a href=\"$replaceLink$1\">$1</a>", $content);
    return $newContent;
    }
    
    //to use it
    print makeLink($content);
    PHP:
     
    ads2help, Oct 18, 2008 IP
  6. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Why use regex? That seems a lot slower than explode
     
    Kyosys, Oct 19, 2008 IP
  7. elias_sorensen

    elias_sorensen Active Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #7
    For example to remove whitespaces, if not, whitespaces would become links.
     
    elias_sorensen, Oct 19, 2008 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #8
    Arguable. A regex in this case is one function call to something written in a compiled language... While the explode itself may be faster, looping through each and every word in php may be slower the more words you are processing...

    For two or three words, explode is likely faster - if you are processing two thousand or so words, using a regular expression is more likely to be the faster approach than iterating through a list in an INTERPRETED language like php.
     
    deathshadow, Oct 19, 2008 IP
  9. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Fair enough. I guess it depends on what the author wants to use this for
     
    Kyosys, Oct 19, 2008 IP
  10. Bind

    Bind Peon

    Messages:
    70
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I disagree.

    explode is exponentially faster than "the pregs/regex". It always will be.

    In fact, the larger the $string being processed becomes, the faster it (explode) becomes in comparison.

    take for instance this benchmarking of $string with a length of 14787 characters using explode/str_replace and preg_replace.

    http://digitalpointforumhelp.no-ip.org/dp_forum_threads/links%20from%20text/

    (sorry for the plaintext url ... the forum wont let me paste live links yet for some reason - which is freaking frustrating as hell when trying to help people - HINT - ADMIN - please turn them on !)

    the test is performed on a commercial virtual server with 8 processors, 10%-12% ram usage on average, with a cpu load of 1.0-1.5 on average, so its being performed on an enterprise-class production server, in case anyone was wondering.

    notice the exponentially lower time that explode() has.

    the 1800%-2900% faster script execution time is considerable.

    here is the source code for the benchmark if anyone is interested:

    
    <?php
    #
    # the string to process
    #
    $string = "Leo gave me my first lessons in ethnography. I'm not sure how he was able to know my job better than I knew it myself. But somehow his innate understanding forced me to see the consequences of my actions as an ethnographer and as a human being. Fieldwork is a slow and sometimes painful process. So much social and emotional angst accompanies trying to get to know the people you want to work with. It involves putting yourself where you do not belong, where you may not be wanted; making painful social mistakes; having to deal with issues of race, trust, honesty, money, class. Ethnographic fieldwork is made up of people with moods and personalities. You have your unlucky days and your lucky days, which makes fieldwork something of an emotional roller coaster.
    
    Working with gangs in the city where one lives is not considered typical anthropological fieldwork. I was a commuter anthropologist. I never lived with the people I worked with but, in typical L.A. style, would drive over to my field site and hang out with them. I thought wishfully of moving to where my informants lived and envied those who could leave the country to immerse themselves in another culture. They didn't have to go back and forth all the time like I did, from real life to anthropological life. Such movement called out the superficiality of the ties that I made with people, how connections to them were driven by fieldwork and were therefore completely unnatural. I struggled with my lack of integration into people's daily lives and, as a result, continually deemed my fieldwork an abject failure in terms of traditional anthropology.
    
    The contradictions that commuter fieldwork presented were difficult for me to reconcile. All anthropologists experience contradictions, whether or not they work in urban settings. But for me, the traveling especially made me continually question the construction of our city. Why did I constantly have to be aware of my own role as cultural oppressor? I was a consumer and had to support the powers that be to live my daily life. It was difficult doing fieldwork knowing that I was the enemy to so many of those whose knowledge I sought. In the long run, this back and forth process proved an important focus to my view of the city, but during fieldwork I considered it a mild form of mental torture.
    
    There were also times, however, when I was grateful to be able to go home. Even though I was afraid of making stupid cultural mistakes (which I did a lot), some of the time I was just plain afraid. Once in Watts I had such a bad time that I was relieved to drive to the lily-white suburb of Torrance where I had been raised. It was a place I had deemed 'cultureless' and had learned to despise. But at least no one stared at me with hate on the street there. How ironic that the place I best fit in was the place I least wanted to be.
    
    As with most major cities, it is the nature of Los Angeles to segregate people. This segregation makes you feel comfortable on your own turf and uncomfortable on somebody else's. Because of the city's size (its famous sprawl), such zones of comfort can be enormous but still manage to exclude entire populations from their midst. I had to develop survival mechanisms for the hatred I encountered when I crossed those boundaries. I certainly felt exhilarated when I did so successfully—when I did fit in and felt welcomed and accepted, and even wanted. Ultimately, the power of those moments made it possible for me to do fieldwork in a city where divides of a few miles sometimes seemed greater than those separating nations. [See a map of the distribution of gangs in the Central Vernon neighborhood, South Central Los Angeles.]
    
    Leo was the first person to help me learn to negotiate those divides successfully, and in the process he taught me how to deal with my own mistakes in the field. Fieldwork is full of anthropologists making mistakes. Some of those mistakes can be costly indeed. But mostly people understand them and try to help you to comprehend the nature of your mistakes. In the process, they help you learn your trade: how to be an anthropologist and how to understand the nature of their culture. I had relatively few dealings with Leo altogether, but even after his death my experiences in the field always seemed to revolve around him or to lead back to him somehow through his family and friends.
    
    I learned many lessons from my first fieldwork. They were so painful that I long resisted putting them to paper. They seemed to expose both the best and worst elements of my personality. My work with the 17th Street gang in Santa Monica was haphazard and unsystematic. It spanned over a period of about four years. It was embarrassing to look back at my glaring mistakes, my unmaintained ties, and the way I flitted in and out of the gang members' lives. I could rationalize my problems away, I know: my informants moved in and out of jail, my fieldwork wasn't all that bad. I had also just started in a graduate program for which I was woefully ill-prepared. But in my heart I knew I could have done better.
       Figure 3.26. North versus South gang graffiti (Delhi, Calif., off Highway 99, 1996)
    Photograph by Susan A. Phillips. All rights reserved.
    
    Figure 3.1. Leo and Lil Trigger, Santa Monica (27 June 1991) Rest in Peace
    Photograph by Susan A. Phillips. All rights reserved.
       It was 1991. I had been taking pictures of graffiti for about six months but had somehow managed to avoid coming into contact with people. I think I was just trying to be careful, to get a feel for the whole street scene. But after all that time and all those photographs, I craved interaction with the people whose work I had documented almost daily. It was nothing other than a freak accident that finally gave me the opportunity to connect with the gang members from the Santa Monica neighborhood close to where I lived. I was on my way to take some more pictures. It was like any other summer day for me—I had my camera and planned to park my car and then walk to an abandoned house that was a popular graffiti target. Instead I drove around the corner to see the flashing lights of police cars and a crowd gathering. A car had driven itself literally up a telephone pole cable and was suspended, rear fender resting on the ground, at a forty-five-degree angle. After somehow extracting himself from this precarious position, the driver had fled, and I think the police were still chasing him when I arrived.
    
    Among those gathered were two gang members distinguishable by their crisp white T-shirts and baggy black pants. I carefully positioned myself next to them as I started taking pictures of the scene. Pretty soon a third, older gang member on a tiny bicycle came riding over. He asked me if I was working for the paper. I said no, but sort of mentioned that I was actually on my way to take pictures of gang graffiti. He said that was cool. One of the younger ones was named Ruben. He and his friend told me that I could meet them later around the corner where they hung out. So I walked off to take a few pictures, and a bunch of little boys ended up giving me a guided tour of their neighborhood. I was grateful to them for their acceptance and company.
    
    Across from the abandoned house was the apartment whose courtyard served as the hangout for the 17th Street gang. When we reached it, I said good-bye to the little boys and told them I would see them again soon. I walked across the street toward the group of gangsters with my heart pounding. I was looking for Ruben, but only saw the older guy who had been on the bike. I went over to him. It was through him that I met Leo.
    
    Somehow Leo was in charge. Either that or he was naturally a little interested in and very suspicious of my project. We talked, he asked me questions, but it seemed from the start he knew exactly what I was doing and, more, how I should be doing it. When I saw his tattoos peaking out from his shirt, I practically begged him for a picture. After he said, 'No, I'm not about to take off my shirt and show you my tattoos,' I kept pushing, asking, 'please, please, are you sure?' Different people came by, curious, asking about my project. Then as Leo was just about to leave, he suddenly lifted up his shirt to pose for me. I was so nervous I could barely focus the camera. One tattoo around his neck read 'I Just Don't Give A Fuck' in the beautiful Chicano gang script; over his heart was another of a rosary and praying hands with an inscription that read 'Pardoname Madre Por Mi Vida Loca.' Forgive me Mother for my crazy life. He also had a big '17' on the back of his neck, and some little 'SM's' and '17's' here and there on his wrists and elbows, for Santa Monica 17th Street. He let me take the pictures at a variety of angles—but made sure never to show his face. A friend of his, Trigger, jumped in at the last minute to cover up his eyes on one of them just to be safe.
    
    As I headed for my car, I heard them shout, 'Hey, we could use a ride!'
    
    I remembered the conversation with my mother just before I left that day. She was in Northern California and knew I was going to a place full of unknowns. The last thing she said to me, half joking, was, 'Don't give anybody a ride!' I said okay. But when Leo and Pelon asked, I knew I wanted to give them a ride. I automatically trusted them, even though my brain told me I should be afraid. But I also hesitated, remembering the promise to my mom. After all, they were gang members. They killed people, didn't they? They committed crimes; they raped people, didn't they? So I refused them.
    
    'Hey, we trusted you!' they responded. 'We let you take pictures of us!'
    
    I managed to say no, that I was just some stupid white girl with a camera, but they, they were real gangsters who could really hurt me if they wanted. I think I kind of flattered them in this way, which is the only thing that made my refusal even semi-acceptable. At the same time I felt confused by not being able to trust my own feelings. How could I negotiate this world if I was supposed to be afraid of the people I wanted to work with? I continually struggled with that issue throughout my fieldwork, but that was the only time I ever refused a ride to anybody if I felt okay about it. Looking back I realize that it's good to do these things once, to help gauge decisions for the future. It's just too bad it had to be with Leo that first time.
    
    The next day I went back there and Leo kind of sauntered over and just let me have it. I guess he had been thinking about me and had become more and more angry. He said I had been way too pushy the day before. Trying to defend myself, I said yeah, but I got what I wanted (i.e., the pictures). He countered that the only reason I got what I wanted was because he had given it to me. He had let me take those pictures, like a present. He asked, was I trying to study them under a microscope? Like they were insects? I said no. I tried to explain about anthropology, how it was learning about different cultures all over the world. But I was embarrassed. I said it would be so much easier if I were Chicana—that I wished I was. But as soon as I said this, Leo said, 'No. Susan, you have to be proud of what you are.'
    
    I tried to know that this was true. Later I figured that this was one of the main things about doing fieldwork I first learned from Leo: you have to accept who you are in order to have others accept you. They were proud of themselves, that's what the gang was all about. I should be proud of myself too. He said he was proud of me because I came down there and wasn't afraid—but that I had been too insistent.
    
    I went home that night almost in tears, feeling like I had made some terrible mistakes during my first experiences, which at the time seemed excruciatingly important. How could I have been so insensitive and disrespectful to them that first day? Being pushy, getting what I wanted, then not even giving them a ride? And that second day being so rude in my defensiveness. I hoped that Leo didn't think too badly of me, and I couldn't blame him if he did. But we were cool after that. He knew I was just beginning and that he was helping me learn. I gave him his pictures, and he liked them.
    
    I got to know some other folks, began interviewing for a methods class I was taking, and had some crazy barrio experiences. I didn't see too much of Leo after those first days, but he was always around me somehow, in the background of my mind. His expectations were something for me to measure up to. From him I learned that you need to trust in order to be trusted. . . .
    
    Representing through Graffiti
    
    Chicano gang members represent their neighborhoods in graffiti in a variety of ways and for a variety of reasons. Their graffiti ranges from the most minute writings on walls and concrete scratchings to larger-than-life gigantic images that require the use of ladders and crates of spray paint. These messages have the ability to stand on their own and act for gang members even when they themselves are not around; their public nature makes them an important locus to position self and group at a distance through shorthand symbols. Graffiti are crucial mechanisms for the acts of representing through which gang members intertwine their emotional and political concerns.
    
    Gang members identify basically four interrelated but distinct types of graffiti: hitting up, crossing out, roll calls, and RIPs (memorial graffiti). They correspond roughly to the categories with which Ley and Cybriwsky (1974) designated Philadelphia graffiti: affirmative and aggressive. Through 'hitting up' and 'roll calls,' gang members make positive statements about group belonging and membership (what Ley and Cybriwsky indicate to be roughly affirmative). Through 'crossing out' or 'challenging,' they engage other gangs in discontinuous dialogues that are ritual struggles for power and recognition within their community (what Ley and Cybriwsky indicate to be aggressive). Constituting the last category are the memorial markers gang members make for homies lost during the course of these struggles; generally they are called 'RIPs.' These categories relate to different elements of gang life and membership. Through the text I link them as often as possible to the social concerns that further bind gang members to their neighborhoods and culture.
    
    Graffiti is one among many gang practices that help gang members define their group within the spaces where they live. Examining graffiti and its links to other types of cultural production through time is a uniquely strong method for charting social and political change. Graffiti—a barrio's visual, tangible history—offers living proof of both changes and continuities in neighborhood concerns.";
    #
    # the create links function that uses explode and str_replace for filtration
    #
    function CreateLinks($string)
        {
            $linktext = explode(" ",$string);
            foreach ($linktext as $this_linktext)
                {
                    $order = array(".","\r","\n","\r\n"," ","'","!","@","\(","\)",",","-","_","—","?");
                    $this_linktext = str_replace($order,"",$this_linktext);
                    $linksout .= "<a href=\"search.php?q=$this_linktext\">$this_linktext</a> ";
                }
            RETURN $linksout;
        }
    #
    # the create links function using preg_replace(\regex\)
    #
    function makeLink($string)
        {
            $replaceLink  = 'http://search.php/?q=';
            $newContent = preg_replace("/([\w]{1,})/","<a href=\"$replaceLink$1\">$1</a>", $string);
            return $newContent;
        }
    #
    #   benchmark explode()
    #
    echo "Baseline \$string (".strlen($string)." characters) both functions will use: <BR><textarea style='height:100px;width:800px;overflow:auto;border:1pb solid black;'>$string</textarea><BR><BR>";
    echo "processing \$string into links with explode():";
       $mtime = microtime();
       $mtime = explode(" ",$mtime);
       $mtime = $mtime[1] + $mtime[0];
       $starttime = $mtime;
    echo "<div style='height:100px;width:800px;overflow:auto;border:1pb solid black;'>" . CreateLinks($string) . "</div>";
       $mtime = microtime();
       $mtime = explode(" ",$mtime);
       $mtime = $mtime[1] + $mtime[0];
       $endtime = $mtime;
       $totaltime = ($endtime - $starttime);
       $explodetime = $totaltime;
    echo "explode() took ".$totaltime." seconds to process \$string into links !<BR><BR>";
    #
    #   benchmarking preg_replace(\regex\)
    #
    echo "processing \$string into links with preg_replace():";
       $mtime1 = microtime();
       $mtime1 = explode(" ",$mtime1);
       $mtime1 = $mtime1[1] + $mtime1[0];
       $starttime1 = $mtime1;
    echo "<div style='height:100px;width:800px;overflow:auto;border:1pb solid black;'>" . makeLink($string) . "</div>";
       $mtime1 = microtime();
       $mtime1 = explode(" ",$mtime1);
       $mtime1 = $mtime1[1] + $mtime1[0];
       $endtime1 = $mtime1;
       $totaltime1 = ($endtime1 - $starttime1);
       $pregtime = $totaltime1;
    echo "preg_replace() took ".$totaltime1." seconds to process \$string into links !<BR><BR>";
    
    echo "--------------------------------------------------------------------------------------------------<BR>";
    echo "Summary:<BR>";
    echo $pregtime . "  = preg_replace with regex script execution time<BR>";
    echo $explodetime . " = explode() script execution time<BR>";
    $exploded = "explode()";
    $pregreplaced = "preg_replace(\\regex\\)";
    if ($exploded > $pregreplaced)
        {
            echo "preg_replace(\\regexp\\) is faster than explode() by " . ($explodetime-$pregtime) . " seconds, or ". round(($explodetime / $pregtime) * 100) . " percent!";
        }
    else
        {
            echo "explode() is faster than preg_replace(\\regexp\\) by " . ($pregtime-$explodetime) . " seconds, or ". round(($pregtime / $explodetime) * 100) . " percent!";
        }
    ?>
    PHP:
     
    Bind, Oct 19, 2008 IP
  11. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #11
    READ CLOSER

    I didn't say the explode wasn't faster. What I said was that the php iteration going through each and every word turned into an array index MIGHT be exponentially slower than just using ONE regex.

    Though I'd have to play with the example - I'm fairly certain between the wasteful variable overheads and the use of the /W that the regex could be sped up a good deal.

    But then the non-regex version has flaws since it should have run-in problems as if the only thing separating two words is a newline or tab, they would be treated as one word - meaning I would do the str_replace on the whitespace characters into spaces FIRST so you could avoid that, though that destroys the formatting and punctuation which might not be acceptable. You could probably do a nested test/explode, but given the stack growth of recursive calls and the overhead of calling functions that would drag you to a crawl - That explode/STR function may be faster as is, but it's buggy as a swamp in july.
     
    deathshadow, Oct 19, 2008 IP
  12. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #12
    Ok, I just went through your little 'benchmark', and you realize your time calculations are flawed, correct? You've got the php5 emulating php4 code kind of 'wrong' there.

    Where you have:

    $mtime1 = microtime();
    $mtime1 = explode(" ",$mtime1);
    $mtime1 = $mtime1[1] + $mtime1[0];
    $starttime1 = $mtime1;

    it should probably be:

    list($microSeconds, $seconds)=explode(" ", microtime());
    $starttime1=((float)$microSeconds + (float)$seconds);

    Because you aren't typecasting to float, your results can't quite be trusted.

    In any case, we now have php5, so there's no reason not to call it thus unless you're stuck with a server that hasn't been updated in two years.

    $startTime=microtime(true);

    That said, I'm getting a bit different results once I cleaned up your code, and minimized other code elements (like bandwidth and handshaking) that could interfere with your result. You are echoing out the results inside your timer - so you can't even trust that since just differences in packet routes and connection saturation over time could make the second one inherently run slower - basically network conditions could completely skew your results. You need to dump that **** into a variable, then echo it AFTER you've taken your time readings.

    So let's clean that up into a MEANINGFUL benchmark. To be fair we'll throw out the function call overhead and any unneccessary variables. We'll move the $order declaration out of the loop to speed up the loop as there's zero reason to be redeclaring the same thing over and over again.

    http://www.cutcodedown.com/for_others/bind/test.phps

    Running live here:
    http://www.cutcodedown.com/for_others/bind/test.php

    Running that three times on my server, I get:

    Iteration 1:
    Summary:
    0.0360009670258 = explode() script execution time
    0.00317597389221 = with regex script execution time
    preg_replace is faster than makeLinksExplode by 0.0328249931335 seconds / 1134 percent!

    Iteration 2:
    Summary:
    0.0226399898529 = explode() script execution time
    0.00313401222229 = with regex script execution time
    preg_replace is faster than makeLinksExplode by 0.0195059776306 seconds / 722 percent!

    Iteration 3:
    Summary:
    0.0226428508759 = explode() script execution time
    0.00317597389221 = with regex script execution time
    preg_replace is faster than makeLinksExplode by 0.0194668769836 seconds / 713 percent!

    The later iterations see a drop because I've got eAccellerator caching php - first iteration was on a clean re-parse.

    This was on a 2ghz Dual P4B with 2 gigs of RAM, a pair of 500 gig SATA mirrored samsungs, Debian Etch, which this time of night on a sunday is hovering around 0.6 CPU load since it's only hosting one major site.

    Again, regex is slow, but not as slow as parsing an array one element at a time in AN INTERPRETED LANGUAGE... and if you take the time to compare the output, you'll notice the regex preserves all whitespace characters/punctuation and does not mess up if non-space characters are the only separators, the explode version does not. Look for what it does to the section that ends with 'coaster' followed by a period and two carriage returns to see why using explode is REALLY made of /FAIL/ for doing this.

    -- edit --
    I found another reason the preg was reporting slower in your original benchmark - because it does not strip punctuation, newlines, tabs or any other characters, it outputs a whopping 30k more HTML. THAT would definately skew your results given the echo was inside the timer... in fact, you were putting http:// before each search link in the preg version but NOT in the explode... Great, so now I correct my code to level the field even more.
     
    deathshadow, Oct 19, 2008 IP
  13. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #13
    There, levelled the playing field to only 8K of difference - though being the two routines do entirely different things, the explode one being 'incorrect'...

    It's now consistantly returning 800 to 850% faster for the regex, with peaks at 1300% and an occasional 'low' of 600%.

    -- edit -- though the regular expression isn't perfect either - notice how it mangles words with apostrophe's...

    -- edit edit -- Fixed the apostrophe mangling by using /S instead of /w, but that may cause problems with periods. I think the REAL answer would be to use a SET or perhaps an OR statement. (putting trapping the exceptions first) - to truly capture all the possible oddball outcomes one may have to resort to a hybrid of the two techniques using preg_split, then iterating each one to perform more in-depth checks - which might not be a bad idea anyways since the output for the links should probably be run through htmlspecialchars.
     
    deathshadow, Oct 19, 2008 IP
  14. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #14
    1 day ago. hmm..
     
    bartolay13, Oct 19, 2008 IP
  15. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #15
    Nice scripts deathshadow
     
    ads2help, Oct 19, 2008 IP
  16. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #16
    Thank you all for the replies.

    I have got another questions.. this is the code i am using now. (it is easy to use for me) How can i ignore punctuation marks on the links? At the moment if there is a sentence like:

    This is a test.

    "test." becomes a link. I want only test to be the link.

    
    <?
    $text = "this is a test";
    $words = preg_replace('/\s\s+/', ' ', strip_tags(trim($text))); 
    $value = '';foreach(explode(' ', $text) as $word){$replaceLink = "<a href=\"search.php?q=".$word."\">".$word."</a>";$processedText .= $replaceLink." ";}
    echo $processedText;?>
    
    PHP:
     
    baris22, Oct 19, 2008 IP
  17. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #17
    That is a mix of regex & explode lol

    
    $content = 'This is a test. This is another test(Test). a1ph4numer1c. ("quote\') !@#$%^&*';
    $replaceLink  = 'http://search.php/?q=';
    $newContent = preg_replace("/([\w]{1,})/","<a href=\"$replaceLink$1\">$1</a>", $content);
    print $newContent;
    
    PHP:
    I tried my version and found that it can deal with that. You didn't try it? ==
    non-words will be auto skipped.

    BTW,
    Example?? *BLUR*
     
    ads2help, Oct 20, 2008 IP
  18. Bind

    Bind Peon

    Messages:
    70
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #18
    indeed, very nice script deathshadow ... i like your example alot ... i hope OP can use it as it's more efficient than mine and ads2help I benchmarked perviously. It nice of you to post some code for OP.
     
    Bind, Oct 20, 2008 IP
  19. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #19
    Ok, I refined it down to handle a great many special cases. Punctuation INSIDE words, underscores, hyphens, etc need to be preserved, but common ending punctuation after words - periods, commas, colons, semi-colons, as well as any 'function' characters like newline or tab, should be stripped from the ANCHOR, but we probably want to preserve that punctuation in the output OUTSIDE the anchors.

    function stringToLinks($stringToProcess) {
    
    	$wordList=preg_split("/\s+/",$stringToProcess);
    	$whiteList=preg_split("/\S+/",$stringToProcess);
    	
    	define("stl_replaceStr",".,/<>?!@#$%^&*()-=_+[]\\{}|;:'\"`~\x00..\x20");
    	
    	for ($t=0; $t<count($wordList); $t++) {
    	
    		$rTrimmedWord=rtrim($wordList[$t],stl_replaceStr);
    		$wordPostPunctuation=substr($wordList[$t],strlen($rTrimmedWord));
    		
    		$strippedWord=ltrim($rTrimmedWord,stl_replaceStr);
    		$wordPrePunctuation=substr($rTrimmedWord,0,
    			strlen($rTrimmedWord)-strlen($strippedWord)
    		);
    		
    		$result.=
    			$wordPrePunctuation.
    			($strippedWord!='' ?
    				'<a href="search.php?q='.urlencode($strippedWord).'">'.
    				htmlspecialchars($strippedWord).
    				'</a>' : ''
    			).$wordPostPunctuation.
    			($t<count($whiteList) ? htmlspecialchars($whiteList[$t]) : '');
    			
    	}
    	
    	return $result;
    	
    }
    Code (markup):
    First we split it into an array of non-whitespace characters using preg_split, but then we split it into an array of the whitespace characters so we can print those out too. We'll also dump our replace string into a define.

    We then iterate through the list - we'll use for instead of foreach since we also need a counter for $whiteList. Next we use rTrim to strip the 'offending' characters from the end of the word, then we use substr to pull the characters we removed so we can still output them. We then reverse this process to pull offending characters from the beginning of the word, but we leave them in the middle of words so things like hypens and apostrophes are preserved.

    Finally we add all this to our result string. We ouput any offending characters we pulled before the word, then we check to see if the word in question even exists. Because of the nature of our split it is possible that our pre or post strings may have values while the word itself is empty! Since we don't want to be bloating the output with empty anchors, we run a quick check before we output the anchor and href - the href gets urlencode run on it to REALLY make sure there are no offending characters passed, and to make sure things like the & sign get passed as a value, not as a delimiter. Next the word itself, close the anchor, toss in any characters we pulled from after the word, and then our whitelist. Just to be on the safe side should there be no characters after the last word, we'll check that out index variable has not exceeded the length of our white-space list. If it is within range, we output said whitespace characters, hitting this too with htmlspecialchars just to be sure we're outputting valid markup.

    Which I think covers most of the bases, though if you run the test:

    echo stringToLinks('
    	It\'s hard to test for every condition... 
    	preg_split might be our solution. (If you know what I mean)
    	This & that, this&that, what\'s the difference?
    ');
    Code (markup):
    You can see that this&that without whitespace is handled as a single word - its' also gramattically incorrect. Something you should be aware of in any case since not everyone uses correct punctuation. We can see though that the output from this is about as safe as you're going to get in terms of preservation:

    
    	<a href="search.php?q=It%27s">It's</a> <a href="search.php?q=hard">hard</a> <a href="search.php?q=to">to</a> <a href="search.php?q=test">test</a> <a href="search.php?q=for">for</a> <a href="search.php?q=every">every</a> <a href="search.php?q=condition">condition</a>... 
    	<a href="search.php?q=preg_split">preg_split</a> <a href="search.php?q=might">might</a> <a href="search.php?q=be">be</a> <a href="search.php?q=our">our</a> <a href="search.php?q=solution">solution</a>. (<a href="search.php?q=If">If</a> <a href="search.php?q=you">you</a> <a href="search.php?q=know">know</a> <a href="search.php?q=what">what</a> <a href="search.php?q=I">I</a> <a href="search.php?q=mean">mean</a>)
    	<a href="search.php?q=This">This</a> & <a href="search.php?q=that">that</a>, <a href="search.php?q=this%26that">this&amp;that</a>, <a href="search.php?q=what%27s">what's</a> <a href="search.php?q=the">the</a> <a href="search.php?q=difference">difference</a>?
    
    
    Code (markup):
    Probably the slowest version presented yet, but that's an inherent problem the more 'special cases' you setup to handle. A better verison would probably use complex sets and conditionals in the preg_split.

    Hope this helps. Language processing, were that it was as simple as people think.
     
    deathshadow, Oct 20, 2008 IP
    baris22 likes this.
  20. Bind

    Bind Peon

    Messages:
    70
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #20
    agree, thats why i stipped out all puncuation. Most times you dont see output like this used for general reading but for other purposes of placing links oin a page, so punctuation and special characters might not be needed at all.

    it all depends on what OP needs it for.

    he say he doesnt want puncuation (at least the periods) so a faster script stripping them all out might be good enough for him.

    even if he doesnt use your latest creation, thanks for posting it. You have shown us some interesting ways to go about he tasks at hand. I know I learned a few things from your scripts to use on newer php vers.


    @ op -

    using functions is easy in this case.

    to use them after they have been included/added to the script is as simple as:

    $linksout = function_name($string);
    PHP:
    $string holds your raw text.

    $linksout hold the links;

    just echo $linksout them where u want on your page.

    i recommend playing with everything shown here then use what works best for you.

    if there is anything you dont understand, do a google search of the function name and it will show you what it does and what it means.

    I know its confusing at first, but once you get a basic understanding of the language, its all alot easier to understand, so you might want to do some php beginners tutorials.
     
    Bind, Oct 20, 2008 IP