Page 1 of 1

Getting a part of a string

Posted: Sat Mar 28, 2009 5:27 am
by BomBas
Hi, I'm having a problem with slicing a string.

I am trying to get a full part from a string, means that I dont want some half words, HTML tags etc.

I tried 2 things, both didn't work.

Code: Select all

$row['desc'] = substr( strip_tags($row['s_desc']), 0, 55 ); // getting half of some words
and:

Code: Select all

    preg_match_all("#\b\s\b#", $row['s_desc'], $matches);
    print_r($matches);
Printing:
Array ( [0] => Array ( [0] => ) ) Array ( [0] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => ) ) Array ( [0] => Array ( [0] => [1] => [2] => [3] => ) )
If you didn't understand, lets say this my string:
I am nobody.Nobody is perfect.Therefore, I AM PERFECT.Word word word word word word word word
When I'm using my first method, It will print something like this:
I am nobody.Nobody is perfect.Therefore, I AM PERFECT.Word word word word word word wo
When I'm using my second method, still an empty array..

Thanks in advance.

Re: Getting a part of a string

Posted: Sat Mar 28, 2009 5:35 am
by php_east
don't understand. which part of the string do you want ?

Re: Getting a part of a string

Posted: Sat Mar 28, 2009 5:42 am
by BomBas
55 charaters from the string, if the 55th char isn't a boundary of a word, I want it to be more than 55 chars for taking all the chars of the current word.

Re: Getting a part of a string

Posted: Sat Mar 28, 2009 7:57 am
by php_east
maybe use str_word_count($str, 1); OR str_word_count($str, 2);