Search code examples
phpstringsubstr

delete a part of string with php


i want delete a part of string. for example i have this string

13|5

and i want to reach the sub string located before the char |. it means i have this 13|5 and i want 13. location of | is unknown. for example maybe the string is 125|354 and again i want 125

<?php foreach($first as $key => $value){
    echo $key."</br>";
    //$key =  13|5//
    // write a function to reach 13//
}?>

Solution

  • You need to use combination of substr() and strpos()

    $str = '125|354';
    $newStr = substr($str, 0, strpos($str, '|'));
    // 125