Welcome Guest, Not a member yet? Register   Sign In
Truncate String
#1

[eluser]Yash[/eluser]
In searching I need to truncate rest of the string after I found a predefined string.

I mean

"Hey coding is fun and I love it."
Now I need to break string after "and" so I want only "hey coding is fun"

My code is
Code:
$content="Hey coding is fun and I love it.";
$contents=explode(' ',$content);
    $viewcontent="";
    print_r($contents);
    for($i=0;$i<=count($contents)-1;$i++)
    {
        if($contents[$i]=="and")
            break;
        else
            $viewcontent.=$contents[$i]." ";
        
    }

Any more idea I mean If I don't want to break string or don't want array..
#2

[eluser]xwero[/eluser]
Code:
$strarray = str_word_count($str, 2);
echo ($max =array_search('and',$strarray))?substr($str,0,$max):$str;
The str_word_count function gets you the array like you did with explode but the second parameter tells the array the key has to be the start position of the word.
And the rest you can figure out yourself Wink

edit : you can't have false as the third parameter of substr Smile
#3

[eluser]Yash[/eluser]
My New Code :

Code:
$findme    = '<|more|>';
    $pos = stripos($content, $findme);

    if ($pos !== false)
    {    $viewcontent= substr($content, 0, $pos);    }
    else
    {    $viewcontent=$content;                        }
    
    echo "Small -> ".$viewcontent;
thanx xwero
#4

[eluser]xwero[/eluser]
You can compact the code
Code:
$findme    = '<|more|>';
if(($pos = stripos($content, $findme)) !== FALSE)
{
    $content= substr($content, 0, $pos);
}
echo "Small -> ".$content;
The else isn't needed as it is the unchanged content.
#5

[eluser]Yash[/eluser]
Bingo..I like that

Thank you
#6

[eluser]Pascal Kriete[/eluser]
I know this is solved, but that just makes me cringe. You can explode at full strings, not just characters:
Code:
$findme = '<|more|>';
$content = current(explode($findme, $content, 2));

echo "Small -> ".$content;
#7

[eluser]xwero[/eluser]
nice!
#8

[eluser]Yash[/eluser]
Which is FASTER ...I'll go for that.

nice coding skill..

Thank you
#9

[eluser]Yash[/eluser]
[quote author="inparo" date="1214497942"]I know this is solved, but that just makes me cringe. You can explode at full strings, not just characters:
Code:
$findme = '<|more|>';
$content = current(explode($findme, $content, 2));

echo "Small -> ".$content;
[/quote]

This one is really fast. I performed benchmark testing.result this code is winner compare to my previous one.

Thank you
#10

[eluser]sophistry[/eluser]
sorry! i couldn't resist.

i benchmarked this at around 20% faster with small strings; there's no need to go into array land at all.

this will stay fast even if the input string $content is huge while the array functions will begin to bog down (even with the limit param of explode()).

Code:
$findme = '<|more|>';
$small = substr($content, 0, strpos($content, $findme));
echo "Small -> ".$content;

EDIT: btw, make sure to use xwero's example of this string-centric technique above because it has some error trapping. that may be why the benchmarks put it slower than inparo's explode() approach.

EDIT: actually i just went back and tested and you don't need error trapping at all in either approach - both nested functions handle varied text input conditions without crying.




Theme © iAndrew 2016 - Forum software by © MyBB