Reputation: 365
I have string that will look like this:
$string = "hello, my, name, is, az";
Now I just wanna echo whatever is there before first comma. I have been using following:
echo strstr($this->tags, ',', true);
and It has been working great, but the problem it only works php 5.3.0 and above. I am currently on PHP 5.2.
I know this could be achieve through regular express by pregmatch but I suck at RE.
Can someone help me with this.
Regards,
Upvotes: 2
Views: 7394
Reputation: 6527
Too much explosives for a small work.
$str = current(explode(',', $string));
Upvotes: -1
Reputation: 146460
<?php
$string = "hello, my, name, is, az";
echo substr($string, 0, strpos($string, ','));
You can (and should) add further checks to avoid substr if there's no ,
in the string.
Upvotes: 8
Reputation: 1008
You can simple use the explode
function:
$string = "hello, my, name, is, az";
$output = explode(",", $string);
echo $output[0];
Upvotes: 1
Reputation: 11830
You can explode this string using comma and read first argument of array like this
$string = "hello, my, name, is, az";
$str = explode(",", $string, 2);
echo $str[0];
Upvotes: 2