Jump to content

[SOLVED] stripping down part of a string


AdRock

Recommended Posts

I am trying to cut down a string up to a certain point but it's not working

 

what i want it to output is

 

test/another-test/here

 

and anything else is stripped off

 

<?php

$string = "test/another-test/here/end";

$string = substr($string,0,stristr($string,'end'));

echo $string;

?>

Link to comment
https://forums.phpfreaks.com/topic/151148-solved-stripping-down-part-of-a-string/
Share on other sites

If the string is static you should use (http://php.net/strpos) but if it's dynamic, you could either use regexp (http://www.regular-expressions.info) or the example below.

 

 

 

<?php
$string = "test/another-test/here/end";
$toFind = "end";
$position = strpos($string, $toFind);

if ($position !== false) {
	echo substr($string, 0, $position);
}
?>

If you want to drop last element, there's another way of doing it:

 

<?php

$string = "test/another-test/here/end";

// Turn string into array
$array = explode('/', $string);

// Remove last array element
array_pop($array);

// Glue array into string
echo implode('/', $array);

?>

 

 

<?php
///try this 


$string =  str_replace('end',' ');
//if you need to cut down more of the script try putting content after the end into a variable 

///example 
$end='posted content';
$string1 =  str_replace('$end',' ');
$string = "test/another-test/here/$string1";
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.