Removing text after string

2019-06-06 10:18发布

问题:

Here is my link

<img src='http://www.example.org/images/post_images/2400.jpg' /><div>© Ham Sammich</div></div>

I want to remove the text after the /> thus making the link..

<img src='http://www.example.org/images/post_images/2400.jpg' />

I've tried using the strstr() function on the end, but my vps does not have the option of going to php5.3 thus making that not a go.

Here is my code, which does not work because of 5.3 not being on there..

$imgTwo = strstr($img, ">", 1);
$img = $imgTwo . ">";

Whats a good substitute for getting this done the way I want?

回答1:

There is alternative solution to the one given by @JonathanRich and it does not involve regular expressions.

$result = array_shift(explode('/>', $string)).'/>';

where $string is your string and $result is the result of processing.

What it means? It says "split the string on "/>" and return part before its first occurence, then add "/>" to it and save as $result". Which means it does exactly what you wanted :)

Here is the demonstration that this actually works: codepad.org/0TTCypA5

There is also alternative solution to above alternative solution :)

$result = substr($string, 0, strpos($string, '/>')+2);

the demonstration of which is here: codepad.org/ifyJiaPB. This one says: "find the position of first occurence of "/>" within the string and return everything before, including "/>" itself".



回答2:

preg_match( '/<img[^>]*>/', $img, $matches);

$matches[0] will be just the img tag.



回答3:

Oh the dark time before strstr()...

Here is a selection of some possible methods:

Code: (Demo with PHP5.2.17)

$string='<img src=\'http://www.example.org/images/post_images/2400.jpg\' /><div>© Ham Sammich</div></div>';

//echo 'strstr: ';
//echo strstr($string,'<div>',true); // not in php5.2

echo "strtok: ";
echo strtok($string,'>'),'>';

echo "\n\nsubstr/strpos: ";
echo substr($string,0,strpos($string,'<div>'));

echo "\n\nexplode no limit: ";
//echo explode('<div>',$string)[0]; // not in php5.2
echo current(explode('<div>',$string));

echo "\n\nexplode limit 2: ";
//echo explode('<div>',$string,2)[0]; // not in php5.2
echo current(explode('<div>',$string,2));

echo "\n\npreg_match: ";
//echo preg_match('/<img[^>]*>/',$string,$out)?$out[0]:$string; // not in php5.2
echo preg_match('/<img[^>]*>/',$string,$out)?current($out):$string;

echo "\n\npreg_split: ";
//echo preg_split('/>\K/',$string,2)[0]; // not in php5.2
echo current(preg_split('/>\K/',$string,2));

echo "\n\npreg_replace: ";
echo preg_replace('/<div>.*/','',$string);

Output:

strtok: <img src='http://www.example.org/images/post_images/2400.jpg' />

substr/strpos: <img src='http://www.example.org/images/post_images/2400.jpg' />

explode no limit: <img src='http://www.example.org/images/post_images/2400.jpg' />

explode limit 2: <img src='http://www.example.org/images/post_images/2400.jpg' />

preg_match: <img src='http://www.example.org/images/post_images/2400.jpg' />

preg_split: <img src='http://www.example.org/images/post_images/2400.jpg' />

preg_replace: <img src='http://www.example.org/images/post_images/2400.jpg' />

  • strstr($string,'<div>',true); -- this would have been the best call -- one function call creates the desired string, no extra function calls, no excess memory usage / no concatenation.
  • strtok($string,'>'),'>' -- this is nearly clean, but not quite because it can only seek a single character and so we must change the target and then concatenate the character at the end.
  • substr($string,0,strpos($string,'<div>')) -- it generates the desired substring from the string, but makes two function calls when just one call is desirable for this simple task.
  • current(explode('<div>',$string)) -- this generates an array of unlimited elements, only to access the first string. It's two function calls and not very clean as it asks for more work to be done than is necessary.
  • current(explode('<div>',$string,2)); -- limiting the elements is better than the previous explode method, but the same principle applies: why make an array when you only want a string.
  • preg_match('/<img[^>]*>/',$string,$out)?current($out):'failed'; -- whether it is preg_match, preg_split, preg_replace, etc. regex patterns are notoriously slower than non-regex functions. For this case, there is no advantage to employing a regex approach. So while it is only one function call, it won't be the most efficient way.

Do your own benchmarking if you like, but performance will be indistinguishable to your end user for such a small string.

The next criteria will be up to you. Perhaps you feel one way is easier to read than another, or perhaps you want a function that doesn't choke easily if the expected delimiter is missing.



标签: php substring