-3

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"

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
joyce_ann
  • 1
  • 3

1 Answers1

0

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.