Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP String Replace with end of string [duplicate]

Tags:

string

php

I need to replace some string to end of string or cut the string when he got ";" sign?

Im trying but it doesnt work :

$string = "Hello World; lorem ipsum dolor";
$string = str_replace(";","\0",$string);
echo $string; //I want the result is "Hello World"
like image 809
GandhyOnly Avatar asked Dec 19 '22 08:12

GandhyOnly


2 Answers

This should work for you:

<?php

    $string = "Hello World; lorem ipsum dolor";
    echo $string = substr($string, 0, strpos($string, ";"));

?>

Output:

Hello World
like image 105
Rizier123 Avatar answered Dec 24 '22 02:12

Rizier123


You need to split the string with ; and get first element from the returned array (it will always have at least one entry).

echo explode(';', $string, 2)[0];

explode()

like image 28
Pupil Avatar answered Dec 24 '22 02:12

Pupil