Home > database >  How do I remove string after or before space in PHP?
How do I remove string after or before space in PHP?

Time:01-10

How do I remove string after or before space in PHP?

I have a string like $string = "Hello World";

The output should be "Hello"

How can I do that?

I have a string like $string = "Hello World";

The output should be "Hello"

CodePudding user response:

Well, you can explode on <space> and take the first element of the resulting array:

<?php

$a = "Hello World";
$arr = explode(" ",$a);
echo $arr[0];  // Hello

I can't help but think there's more to it than this.

  • Related