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

[eluser]xwero[/eluser]
ok i will check it out, it's friday anything can happen Wink
#22

[eluser]xwero[/eluser]
i used this code to check
Code:
$in = "Hey coding is fun <|more|> I love it.";
        $in = preg_replace('/\<\|more\|\>.+/','',$in);
        echo "Small -> ".$in.'<br>';
        // missing
        $in = "Hey coding is fun I love it.";
        $in = preg_replace('/\<\|more\|\>.+/','',$in);
        echo "Small -> ".$in.'<br>';
And if got me this result
Quote:Small -> Hey coding is fun
Small -> Hey coding is fun I love it.
It works as expected. Can you show the code you used to check it?
#23

[eluser]Yash[/eluser]
my bad..

This time it works nicely.I guess this is best time saving algo.

mate you are really champ...

Thank you so much xwero.
#24

[eluser]Yash[/eluser]
Your code perfectly with one line..If I change my content to

"<p>Expert Vision is a free, tableless, W3C-compliant web design layout by <span>Speedovation</span>.
This template has been tested and proven compatible with all major
browser environments and operating systems. You are free to modify the
design to suit your tastes in any way you like.</p>
<|more|>
<p>We only ask you to not remove <span>"Design by Speedovation"</span> and the link http://www.speedovation.com from the footer of the template.</p>
<p>If
you are interested in seeing more of our free web template designs feel
free to visit our website, Speedovation. We intend to add at least 25
new free templates in the coming month.</p>"

now it fails........showing all values
#25

[eluser]xwero[/eluser]
Code:
preg_replace('/\<\|more\|\>.+/m','',$in);
should work

regular expressions are the perl inside php, one line packs a whole lot of power but you can only understand it if you know the language Smile
#26

[eluser]Yash[/eluser]
I know VB.NET regular expression and a little in php ...

anyways thank you bro...
#27

[eluser]Yash[/eluser]
not working... sorry to make u mad
#28

[eluser]xwero[/eluser]
it's s instead of m, so i own you an appology
#29

[eluser]sophistry[/eluser]
I wrote up a test file. it is at http://codeigniter.com/wiki/String_Parsing_Test_Code/.

conclusion:
------------------
substr() approach is best overall - it is fast with small strings (preg_replace is the fastest).
substr() stays fast no matter how large the input string gets (the other techniques slow with progressively longer strings).

here are the four approaches i tested:
Code:
function preg_rep_cut($in,$split_at_regex)
{
    return preg_replace($split_at_regex, '', $in, 1);
}

function preg_split_pre_cut($in,$split_at_regex)
{
    return current(preg_split($split_at_regex, $in, 2));
}

function substr_cut($in, $splitat)
{
    $out = (substr($in, 0, strpos($in, $splitat)));
    return $out?$out:$in;
}

function explode_cut($in, $splitat)
{
    return current(explode($splitat, $in, 2));
}
sample output from the code:
Code:
Array
(
    [/\<\|more\|\>.*+/si] => Array
        (
            [0] => 0.0064
            [10] => 0.0072
            [200] => 0.0086
            [400] => 0.0087
            [800] => 0.0091
            [1600] => 0.0385
            [3200] => 0.1111
        )

    [/\<\|more\|\>/s] => Array
        (
            [0] => 0.0066
            [10] => 0.0068
            [200] => 0.0095
            [400] => 0.0095
            [800] => 0.0098
            [1600] => 0.0105
            [3200] => 0.0755
        )

    [substr] => Array
        (
            [0] => 0.0071
            [10] => 0.0073
            [200] => 0.009
            [400] => 0.0089
            [800] => 0.009
            [1600] => 0.009
            [3200] => 0.009
        )

    [explode] => Array
        (
            [0] => 0.006
            [10] => 0.0064
            [200] => 0.0107
            [400] => 0.0132
            [800] => 0.0138
            [1600] => 0.0141
            [3200] => 0.0809
        )

)
Array
(
    [substr] => 0.00847142857143
    [/\<\|more\|\>/s] => 0.0183142857143
    [explode] => 0.0207285714286
    [/\<\|more\|\>.*+/si] => 0.0270857142857
)

enjoy. :coolsmile:




Theme © iAndrew 2016 - Forum software by © MyBB