Fork me on GitHub

Php strings help  Bottom

  • How do i do this eg:

    echo " Hello world.How are you ?";

    I want to seperate the strings eg output:

    word1:Hello
    word2:world
    word3:How
    word4:are
    word5:you

    Thanks Guys
  • I'm not sure what you mean. Do you want to know how you should echo strings in modules? If all you want to do is echo a single line the easiest way is to just make a function and use return, i.e:

    Code

    function Example_admin_main()
    {
        return "Hello world. How are you?";
    }


    But for more complex HTML you should look into using pnHTML or pnRender.

    If you are asking how you would split up a string by using a common denominator, then there's is a PHP function called explode that you can use:

    http://uk.php.net/explode

    I.e:

    Code

    $string = "Hello world.How are you?";
    $words = explode(" ", $string);
    print_r($words);


    Would write something like this to the screen:
    [1] => Hello
    [2] => world.How
    [4] => are
    [5] => you?

    But as you can see this leaves in the punctuation marks which i'm guessing you don't want, you could try to remove them using str_replace but it could lead to problems with malformed sentences. Also explode will only take one common denominator, so as you see where you have the full stop and no space it isn't broken apart properly. I suspect someone must have written a function to disect sentences to a much better extent than the example I gave, but you only gave this as an example and so disection of sentences may not even be what you want.
  • Yes thank you!!!

    I manage to do that but i still need to seperate the string from puntuations,comas and fullstops.How do i do that?In other words i only want to extarct only words.

    do i need to do this :
    $string='hello world.How are you?';
    $temp = Explode(" ", $string);
    $temp2= Explode(", ", $temp);
    $temp3 = Explode(" ?", $temp2);

    Actually my objective is to pass the sepaerated words to a search function.


    Eg:


    Echo "Hello world. How are you?";

    Output on web browser:

    Hello world. How are you?


    As you see,only the words got URL link.Thats wat i want to do.I only want to extract the words so that i can insert a URL.

    Thank you ...i appreciate it
  • THis is really beyond the scope of these forums, you may be better served asking in a more nebulous PHP forum, like the PHP forum over at http://www.sitepoint.com

    --
    Home Page | Find on Facebook | Follow on Twitter
  • Hi,

    Sorry but actually i'm developing a module for postnuke.I thought you guys could give a hand.

    Thank you.
  • Well if you have a pretty recent version of PHP you can use the str_word_count function.

    Code

    <?php
    $string = "Hello world.How are you?";
    $words = str_word_count($string, 1);

    echo "<center>[ ";

    for($x = 0; $x < count($words); $x++)
    {
        echo $words[$x] . " ";
        if ($x < count($words)-1) echo "| ";
    }

    echo "]</center>";
    ?>


    Will output:

    Quote

    [ Hello | world | How | are | you ]

    It would be quite easy to add links to that example, but as you see it looks slightly odd how the 'How's' first letter is capatalised, so you may want to consider using strtolower to make it uniform.

    Of course chaning this example to use pnRender would look soemthing like:

    Code

    function Example_admin_main()
    {
        $pnRender =& new pnRender('pnLibrary');

        $string = "Hello world.How are you?";
        $words = str_word_count($string, 1);
        $pnRender->assign('words', $words);

        return $pnRender->fetch('Example_admin_showlinks.htm');
    }


    And then showlinks.htm:

    Code

    <center>[
    <!--[foreach name=wordsloop from=$words item=word]-->
    <!--[$word]-->
    <!--[if $smarty.foreach.wordsloop.last == false]-->| <!--[/if]-->
    <!--[/foreach]-->
    ]</center>


    Of course if you have an older version of PHP you may not have str_word_count but you may want to look at the PHP page as some people have posted other ways of doing it with more common functions in the comments.

    Alternativly building on the code I gave above, you could just use str_replace to remove all common punctuation first, then explode and strip out all the blank entries. I.e:

    Code

    $punc = array(".", ",", ";", ":", "(", ")", "!", "?", "&");

    $string = "Hello world.How are you?";
    $string = str_replace($punc, " ", $string);
    $words = explode(" ", $string);
    $words = array_filter($words);

This list is based on users active over the last 60 minutes.