Remove string before a point in PHP

2

Is there any way to remove a string before a point in PHP? I have the following:

1. paqueteNombre.appNombre
2. paqueteNombre2.appNombre2

and I would like to have only the name of the app.

I still can not find documentation on how to carry it out. Thank you very much.

    
asked by Julian Casas 10.04.2017 в 21:31
source

3 answers

4

deal with:

substr($str, strrpos($str, '.')+1);

strrpos what it does is to return the position of the last instance that you are looking for, in this case a point. You use +1 to get everything that follows after the last point.

Greetings.

    
answered by 10.04.2017 / 21:33
source
0

You could use the preg_split function to divide your chain into as many parts as there are points in your chain. As you are going to get an array and the name of your application is going to be last, you can always take the last position - 1 (remember that arrays always start at position 0).

In this way, you should not worry about how many points there are ahead of the name of your app since you will always get the last String of the string, which will correspond to the name of your application.

Example:

<?php
    $string = "paqueteNombre.appNombre";
    $array = preg_split("/[.]/", $string);
    echo $array[count($array) - 1]; //appNombre


    $string2 = "cualquiercosa.otracosa.paqueteNombre.appNombre2";
    $array2 = preg_split("/[.]/", $string2);
    echo $array2[count($array2) - 1]; //appNombre2
    
answered by 10.04.2017 в 21:44
0

You could try explode:

$str = "paqueteNombre.appNombre";
$result = explode('.',$str);
echo $result[1];
    
answered by 10.04.2017 в 21:55