1

I want to split words from string. for example, my string is "In the #name of god" and i need the "name" only!! but when i use this snipet, return me "name of god"

$string = "In the #name of god";    
$word   = explode( '#', $string );
echo $word;
4
  • 1
    $word is array, when you echo it you won't see name of god Commented Apr 5, 2017 at 12:58
  • you are using export here in wrong context. The explode() function breaks a string into an array. Commented Apr 5, 2017 at 12:59
  • @u_mulder, Array ( [0] => In the [1] => name of god ) Commented Apr 5, 2017 at 13:03
  • I suggest using a regex function to match on the regular expression #\w+ where \w matches "word" characters (a-z, 0-9, and _). Hope this helps. Commented Apr 5, 2017 at 14:30

5 Answers 5

5
$string = "In the #name of god";

// Using `explode`
$word   = @reset(explode(' ', end(explode( '#', $string ))));
echo $word; // 'name'

// Using `substr`
$pos1 = strpos($string, '#');
$pos2 = strpos($string, ' ', $pos1) - $pos1;
echo substr($string, $pos1 + 1, $pos2);  // 'name'

Note: The @ character before the reset function is an Error Control Operators. It avoid to display a warning message when using end function with a non-reference variable, and yes, it's a bad practice. You should create your own variable and pass to end function. Like this:

// Using `explode`
$segments = explode( '#', $string );
$segments = explode(' ', end($segments));
$word = reset($segments);
echo $word; // 'name'
Sign up to request clarification or add additional context in comments.

Comments

1

try regex and preg_match

$string = "In the #name of god";
preg_match('/(?<=#)\w+/', $string, $matches);

print_r($matches);

Output:

Array ( [0] => name )

4 Comments

This also returns an array? a string was requested in the question
@RiggsFolly yes, but $matches[0] will have required string and the complexity is very less in this.
also would suggest to use preg_match_all to get all the occurances
@knets yes that's true
0

EDIT

Sorry, I just readed it wrong.

Explode converts an String into Array. So your output'll result in ["In the ", "name of god"]. If you want to catch an word on it you need to be more specific on how it'll work. If you just want to catch the first word after a hashtag you should use strpos and substr.

$string = "In the #name of god";
$hashtag_pos = strpos($string, "#");
if($hashtag_pos === false)
    echo ""; // hashtag not found
else {
    $last_whitespace_after_hashtag = strpos($string, " ", $hashtag_pos);
    $len = $last_whitespace_after_hashtag === false ? strlen($string)-($hashtag_pos+1) : $last_whitespace_after_hashtag - ($hashtag_pos+1);
    echo substr($string, $hashtag_pos+1, strpos($string, " ", $len));
}

3 Comments

This definitely not returns name
@Daan Fixed it.
None of my business but returns #name rather than name
0

There are a couple options (also preg_match would help for multiple instances of '#')

<?php
//With Explode only (meh)
$sen = "In the #name of god";
$w = explode(' ', explode('#',$sen)[1])[0];

echo $w;

//With substr and strpos
$s = strpos($sen , '#')+1; // find where # is
$e = strpos(substr($sen, $s), ' ')+1; //find i
$w = substr($sen, $s, $e);

echo $w;

//with substr, strpos and explode
$w = explode(' ', substr($sen, strpos($sen , '#')+1))[0];
echo $w;

Comments

0

In my own project, I would certainly avoid making a handful of function calls to replicate what can be done with one preg_ function call.

Effectively, match the literal #, then "forget" it with \K, then match one or more non-whitespace characters. The fullstring match is accessed by index 0 if there is a match in the string.

Code: (Demo)

$string = "In the #name of god";

echo preg_match('~#\K\S+~', $string, $match) ? $match[0] : '';
// output: name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.