How to pick up content within a string up to a "bar/" character

Asked

Viewed 17,172 times

12

I got the following String:

$link = '13542345/essa_e_minhastring';

How do I take only the value up to the "/" bar and ignore the rest ?

I would like to assign this value to a variable.

I’m using the following code, but it just breaks the string...and does not separate into a variable as I need it:

 $cod = str_replace("/","<br>",$link);
 echo $cod;

The result is:

13542345
essa_e_minhastring

I need it to be just:

13542345

3 answers

17


You can break the string by / using the explode() and then just use the first part like this:

$partes = explode("/", $link);
echo $partes[0]; // dá 13542345

Example: https://ideone.com/oAnXfR

Another alternative is use regex thus:

$link = '13542345/essa_e_minhastring';
$pattern = '/[^\/]+/';
preg_match($pattern, $link, $partes, PREG_OFFSET_CAPTURE);
echo $partes[0][0]; // dá 13542345

Example: https://ideone.com/4GGZbY



I made a test between explode, preg_match, strtok and substr. The test certainly fails because it is so simple and isolated, and therefore not real. But it is interesting. The result among the 4 methods is:

7.9003810882568E-8 sec / por explode
2.0149707794189E-7 sec / por preg_match // <- mais rápida
4.8128128051758E-8 sec / por strtok
5.2464962005615E-8 sec / por substr
  • 1

    Thank you very much, it fell like a glove.

  • And if I want to give one explode in the value of a array ? It returns an error stating that it expects a string and received a array, how to proceed in that case??

  • 1

    @Marcoshenzel explode works on strings only.

13

The function strstr(), breaks a string by a delimiter and returns the right part by default, by passing the third argument as true the return is the left part.

$link = '13542345/essa_e_minhastring';
echo strstr($link, '/', true);

Another option is explode(), it transforms the string into an array, just take the first element(Indice zero).

This syntax works from 5.4

$link = '13542345/essa_e_minhastring';
$numero = explode('/', $link)[0];
echo $numero;

for lower versions do:

$link = '13542345/essa_e_minhastring';
$numero = explode('/', $link);
echo $numero[0];

Example - ideone

  • 1

    Thank you very much for the reply, yours is also correct, I chose the previous reply as it was posted first.

  • 1

    +1 Now yes ;)

  • 1

    @Bacco has a lot of php code that is oriented to haha array sometimes ends up getting some addictions.

7

Use the substr function();

It takes the content up to the bar and continues as a string;

$text = "123456/efineinfe";
$text = substr($text, 0, strpos($text, "/"));
  • 3

    +1 by the simplest and most direct way, using basic functions of string instead of more "expensive" operations. Just be careful not to include superfluous information in the reply, nor personal data that do not concern what was asked.

  • @Bacco was testing different variants and preg_match seems to be the fastest. You can take a look?

  • @Sergio complicated measure in this case, because preg_match may be caching the expression and reusing, gaining speed from 2nd iteration onwards (this is just a theory).

  • @Bacco yes, even putting the expression statement inside the loop gives me faster. But yes, true, it’s too isolated a case to be sure.

  • 2

    @Sergio the number of internal operations of a Regex compilation is much higher than that of a substring search, in compensation the execution is fast by turning into a state machine in most implementations, but as it is PHP, it is complicated to rely on the implementation of qq one of the 2 things :P

  • That’s right, it’s not an accurate and not so reliable check.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.