How to get the string before "_" in PHP

ReynierPM

I need to get the string before _, but what I've tried is not working properly. The following is a list of input values stored in $b var:

vendor_code
vendor_name
hotel_name_0
hotel_room_type_0
hotel_in_date_0
...
vendor_code
vendor_name
hotel_name_N
hotel_room_type_N
hotel_in_date_N

This is what I have tried:

$a = [
    'vendor_code',
    'vendor_name',
    'hotel_name_0',
    'hotel_room_type_0',
    'hotel_in_date_0'
];

foreach ($a as $b) {
    echo substr($b, 0, -(strlen(strrchr($b, '_')))), PHP_EOL;
}

The code works almost perfectly, but for those, where you have not the ending _N it fails, because it is removing part of the original string (see the output below).

vendor 
vendor 
hotel_name 
hotel_room_type 
hotel_in_date

A valid output should be as follow:

vendor_code
vendor_name
hotel_name 
hotel_room_type 
hotel_in_date

This means, I need to remove all the content after the last _N.

Can any give me some advice here?

RomanPerekhrest

Just remove the ending part _N(if occurs) using preg_replace function:

...
foreach ($a as $word) {
    echo preg_replace("/_\d+$/", "", $word). PHP_EOL;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get string before hyphen

From Dev

php - Get string before nth dash (-)

From Dev

How to get the index of space before specific string?

From Dev

Swift : How to get the string before a certain character?

From Dev

How to extract string presents before comma in php

From Dev

PHP - How to put named variables in string With the string defined before the variables

From Dev

PHP - How to put named variables in string With the string defined before the variables

From Dev

How get the end of string in php?

From Java

How would I get everything before a : in a string Python

From Dev

how to get value before and behind decimal point in string

From Dev

django app how to get a string from a function before

From Dev

How can I get a after and before match in a string?

From Dev

How to get the characters before the last “-” only if the string after the last "-" is numeric?

From Dev

PHP: how to add substring right before first occurrence of another string?

From Dev

How to add a comma for each string result and an "and" before the last result on PHP

From Dev

how to find the characters before and after a word in a string in php

From Dev

PHP: how to add substring right before first occurrence of another string?

From Dev

How to get the last three digit before a character in PHP

From Dev

PHP - How Get text before tag By preg_match?

From Dev

php how to get reference in closure before the object is defined

From Dev

Get length of string before Dot "."

From Dev

Get nedele out of string before

From Dev

BASH: Get text before string

From Dev

Get number before a specified string

From Java

How to get the last char of a string in PHP?

From Dev

How to get the first character of string in php

From Dev

php how to get a string in a specific place

From Dev

How get specific string words in PHP?

From Dev

How to get String after needle in PHP?

Related Related

  1. 1

    How to get string before hyphen

  2. 2

    php - Get string before nth dash (-)

  3. 3

    How to get the index of space before specific string?

  4. 4

    Swift : How to get the string before a certain character?

  5. 5

    How to extract string presents before comma in php

  6. 6

    PHP - How to put named variables in string With the string defined before the variables

  7. 7

    PHP - How to put named variables in string With the string defined before the variables

  8. 8

    How get the end of string in php?

  9. 9

    How would I get everything before a : in a string Python

  10. 10

    how to get value before and behind decimal point in string

  11. 11

    django app how to get a string from a function before

  12. 12

    How can I get a after and before match in a string?

  13. 13

    How to get the characters before the last “-” only if the string after the last "-" is numeric?

  14. 14

    PHP: how to add substring right before first occurrence of another string?

  15. 15

    How to add a comma for each string result and an "and" before the last result on PHP

  16. 16

    how to find the characters before and after a word in a string in php

  17. 17

    PHP: how to add substring right before first occurrence of another string?

  18. 18

    How to get the last three digit before a character in PHP

  19. 19

    PHP - How Get text before tag By preg_match?

  20. 20

    php how to get reference in closure before the object is defined

  21. 21

    Get length of string before Dot "."

  22. 22

    Get nedele out of string before

  23. 23

    BASH: Get text before string

  24. 24

    Get number before a specified string

  25. 25

    How to get the last char of a string in PHP?

  26. 26

    How to get the first character of string in php

  27. 27

    php how to get a string in a specific place

  28. 28

    How get specific string words in PHP?

  29. 29

    How to get String after needle in PHP?

HotTag

Archive