-2

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//
}?>
Mohammad
  • 21,175
  • 15
  • 55
  • 84
sajadsholi
  • 173
  • 1
  • 3
  • 12
  • Hope this link help you. https://stackoverflow.com/questions/6919381/use-regex-or-php-function-to-get-all-characters-before-colon – Yogendrasinh Nov 17 '18 at 09:35

2 Answers2

1

Try explode('|', $string). Hope this helps.

entoniperez
  • 544
  • 5
  • 14
1

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

$str = '125|354';
$newStr = substr($str, 0, strpos($str, '|'));
// 125
Mohammad
  • 21,175
  • 15
  • 55
  • 84