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

[eluser]xwero[/eluser]
sophistry i've made the same mistake with my code. If the string isn't found strpos will return false and that is not a valid third parameter for substr. inparos solution has everything, only using functions.
#12

[eluser]xwero[/eluser]
Are you sure? i tested it and i got an error when the substr has false as the third param
#13

[eluser]sophistry[/eluser]
@xwero - what error reporting level?

here's what i tested both approaches against:
Code:
error_reporting(E_ALL);
ini_set('display_errors',TRUE);
// regular
$in = "Hey coding is fun <|more|> I love it.";
// missing
$in = "Hey coding is fun I love it.";
// empty string
$in = '';
// wrong type
$in = FALSE;

these all worked without choking.

what errors do you see?

EDIT: maybe a PHP version thing? i'm still on 4.
#14

[eluser]xwero[/eluser]
It's not actually an error that is shown but the $in missing the $findme string should return the whole string instead an empty string.
#15

[eluser]Pascal Kriete[/eluser]
EDIT: Nevermind.
#16

[eluser]Yash[/eluser]
You all make me confuse...So which is best code for long text.
#17

[eluser]xwero[/eluser]
To add to your confusion i want to add one more solution Smile
Code:
$content = preg_replace('/\<\|more\|\>.+/','',$content);
One function should beat all the other solutions :coolsmile:
#18

[eluser]Yash[/eluser]
ohhh !!!

it makes me laughing...

anyway Now please test these code and lemme know which is best.

It appears this last function is best because you are taking substring out a string with findmestring. anyways nice work xwero
#19

[eluser]xwero[/eluser]
oops, sorry it's still a bit sleepdrunk.

the $findme string isn't needed anymore. The regular expression will check if the <|more|> string is in the $content string and if it's followed by more text. This has one advantage over all the other solutions and that is if the <|more|> string is at the end of a string it will be visible if the people who add the string see it they know they did something wrong.

if the string is a word like and, the word should start and end with a space otherwise it could be possible the $content string is broken when there is a word like hand in it.
Code:
$content = preg_replace('/ and .+/','',$content);
#20

[eluser]Yash[/eluser]
both the codes are not giving accurate result...showing all text.There is some prob with this.




Theme © iAndrew 2016 - Forum software by © MyBB