Jump to content

Manipulating a String


dyluck

Recommended Posts

HI there,

 

Has anyone the answer to this question?

 

Lets say I have a big string

 

$string = "The quick brown fox jumps over the moon to discover that it really wasn't the moon at all but a big rock in the ground";

$delimiter = "discover";

$maxlenth = 15;

 

I want to limit the charaters before and after the delimiter by 15 charaters (as an example).

DESIRED RESULT:  "er the moon to discover that it really"

 

Thanks for your help!

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/202658-manipulating-a-string/
Share on other sites

you could do something like this:

 

<?php
$string = "The quick brown fox jumps over the moon to discover that it really wasn't the moon at all but a big rock in the ground";
$delimiter = "discover";
$maxlenth = 15;
$new = substr($string,strpos($string,$delimiter)-$maxlenth,$maxlenth*2+strlen($delimiter));
?>

you could do something like this:

 

<?php
$string = "The quick brown fox jumps over the moon to discover that it really wasn't the moon at all but a big rock in the ground";
$delimiter = "discover";
$maxlenth = 15;
$new = substr($string,strpos($string,$delimiter)-$maxlenth,$maxlenth*2+strlen($delimiter));
?>

 

Works like a charm! thank you so much!!!! This helped a lot and coudln't seem to get my head around it lol!

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.