fishmong3r
fishmong3r

Reputation: 1434

get a certain part of a string

I have strings having the below format:

the_part_i_need.abc.xyz.def.uvw

Neither the part I need nor the rest have no fixed lenght. So somehow I have to find the first . and count the string I don't need and then remove it.

substr("the_part_i_need.abc.xyz.def.uvw", -?);

How should I find the first dot and count the number of chars from that point?

Upvotes: 0

Views: 54

Answers (3)

PHP Learner
PHP Learner

Reputation: 111

Try this,

   $pos = strpos($string, '.');

  echo $v1=substr($string, $pos); /*Output : .abc.xyz.def.uvw */

  echo $count=strlen($v1); /* Output : 16  */
 ?>

Upvotes: -2

Wezy
Wezy

Reputation: 667

If you just need the part and not the position you could do it with the explode function.

$string = 'the_part_i_need.abc.xyz.def.uvw';
$parts = explode('.', $string);
echo $parts[0];

Upvotes: 2

mariobgr
mariobgr

Reputation: 2201

$string = 'the_part_i_need.abc.xyz.def.uvw';

$pos = strpos($string, '.');

echo substr($string, 0, (strlen($string) - $pos) -1);

Upvotes: 2

Related Questions