Getting a part of a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Getting a part of a string

Post 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.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Getting a part of a string

Post by php_east »

don't understand. which part of the string do you want ?
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: Getting a part of a string

Post 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.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Getting a part of a string

Post by php_east »

maybe use str_word_count($str, 1); OR str_word_count($str, 2);
Post Reply