If i have a string like this:
$myString = "input/name/something";
How can i get the name to be echoed? Every string looks like that except that name and something could be different.
asked Sep 12, 2009 at 14:09
so the only thing you know is that :
- it starts after input
- it separated with forward slashes.
>
$strArray = explode('/',$myString);
$name = $strArray[1];
$something = $strArray[2];
answered Sep 12, 2009 at 14:11
pixelinepixeline
17.6k11 gold badges84 silver badges109 bronze badges
0
Try this:
$parts = explode('/', $myString);
echo $parts[1];
This will split your string at the slashes and return an array of the parts.
Part 1 is the name.
answered Sep 12, 2009 at 14:11
André HoffmannAndré Hoffmann
3,5051 gold badge24 silver badges39 bronze badges
1
If you only need «name»
list(, $name, ) = explode('/', $myString);
echo "name is '$name'";
If you want all, then
list($input, $name, $something) = explode('/', $myString);
answered Sep 12, 2009 at 14:16
raspiraspi
5,8923 gold badges34 silver badges50 bronze badges
0
use the function explode('/')
to get an array of array('input', 'name', 'something')
. I’m not sure if you mean you have to detect which element is the one you want, but if it’s just the second of three, then use that.
answered Sep 12, 2009 at 14:12
TesserexTesserex
17.1k5 gold badges65 silver badges105 bronze badges
You’ve got to read the manual a few hundred times and it will eventually come to you.
Otherwise, what you’re trying to capture can be expressed as «look for ‘jac’ followed by 0 or more letters* and make sure it’s not preceded by a letter» which gives you: /(?<!\w)(jac\w*)/i
Here’s an example with preg_match_all()
so that you can capture all the occurences of the pattern, not just the first:
$q = "/(?<!\w)(jac\w*)/i";
$str = "I bought a jacket yesterday.
Jack is going home.
I want to go to Jacksonville.";
preg_match_all($q,$str,$matches);
print_r($matches[1]);
- Note: by «letter» I mean any «word character.» Officially, it includes numbers and other «word characters.» Depending on the exact circumstances, one may prefer w (word character) or b (word boundary.)
You can include extra characters by using a character class. For instance, in order to match any word character as well as single quotes, you can use [w']
and your regexp becomes:
$q = "/(?<!\w)(jac[\w']*)/i";
Alternatively, you can add an optional 's
to your existing pattern, so that you capture «jac» followed by any number of word characters optionally followed by «‘s»
$q = "/(?<!\w)(jac\w*(?:'s)?)/i";
Here, the ?:
inside the parentheses means that you don’t actually need to capture their content (because they’re already inside a pair of parentheses, it’s unnecessary), and the ?
after the parentheses means that the match is optional.
To Get Second And Third Word from Strings in PHP we use PHP’s inbuilt function named as explode() function. Before we check the source code to get any word from any position using PHP, we do some introduction about PHP explode function.
Introduction to Explode Function in PHP
The explode()
function breaks the given string into the array.
Syntax of Explode function
explode(separator,string,limit)
- Separator must not be blank.
- Any type of string.
- Limit of an array element according to your string.
Example:
// zero limit $str = ‘php,coder,tech,explode’; print_r(explode(‘,’,$str,0)); print_r(explode(‘,’,$str,2)); |
Array ( [0] => php,coder,tech,explode )
Array ( [0] => php [1] => coder,tech,explode )
To know more about PHP explode function you can check here: https://phpcoder.tech/php-explode-function/
Now we check how to get Second And Third Word from Strings in PHP,
Source code to get Second And Third Word from Strings in PHP
<?php $myvalue = ‘Test Word COUNT in PHP’; $arr = explode(‘ ‘,trim($myvalue)); echo «Second Word: «.$arr[1]; echo «<br>»; echo «Third Word: «.$arr[2]; |
Output:
Second Word: Word Third Word: COUNT |
PHP Get the First 5 Words From String
<?php $dummyText = «Lorem ipsum dolor sit amet consectetur adipisicing elit»; echo implode(‘ ‘, array_slice(str_word_count($dummyText, 2), 0, 5)); |
Output:
Lorem ipsum dolor sit amet
Code Explanations:
- We use
str_word_count()
function to check an array where the key is the position of the word in the string, and the value is the actual word. array_slice()
it returns 1 to 5 words from the given string.
Check the complete use and explanations of explode function from the PHP official site, https://www.php.net/manual/en/function.explode.php
You can check all the code using our online compiler, https://phpcoder.tech/php-online-editor/
Also Check:
- Export MySQL Data to Excel in PHP Using Ajax
- Login with Google Account using PHP Step by Step
- How to Embed PDF in Website Using HTML
- 2 Ways to Open URL in New Tab Using JavaScript
Happy Coding..!
Was this article helpful?
YesNo
Hi Dev,
In this example, you will learn php find specific word from string example. I’m going to show you about how to find specific word from string in php?. This tutorial will give you simple example of find specific word from string in php example. step by step explain how to use function to find specific word from string in php? .
In this example to php find specific word from strings in PHP. in this example, we will use to explode() function used to find specific word. so Let’s see the following example with output.
Example: Find Specific Word from String
index.php
<?php
$str = "I am PHP Developer";
// Get Specific Word from string
$strArray = explode(' ',$str);
$result = $strArray[2];
echo $result;
?>
Output:
PHP
I hope it could help you…
#PHP
✌️ Like this article? Follow me on Twitter and Facebook. You can also subscribe to RSS Feed.
The first word of a sentence can be obtained with the help of various inbuilt functions like strtok(), strstr(), explode() and some other methods like using regular expressions. Some examples to get the first word of a sentence are listed below:
Method 1: Using strtok() Function: This function is used to tokenize a string into smaller parts on the basis of given delimiters. It takes input String as an argument along with delimiters (as second argument).
Syntax:
string strtok( $string, $delimiters )
Program:
<?php
$sentence
=
'Welcome to GeeksforGeeks'
;
echo
"The first word of string is: "
.
strtok
(
$sentence
,
" "
);
?>
Output:
The first word of string is: Welcome
Method 2: Using trim() and explode() Function:
- trim() Function: The trim() function is used to remove the whitespaces and also the predefined characters from both sides of a string that is left and right.
Syntax:
trim( $string, $charlist )
- explode() Function: The explode() function is used to split a string in different strings. The explode() function splits a string based on the string delimiter, i.e. it splits the string wherever the delimiter character occurs. This function returns an array containing the strings formed by splitting the original string.
Syntax:
array explode( separator, OriginalString, NoOfElements )
Program:
<?php
$sentence
=
'Welcome to GeeksforGeeks'
;
$arr
=
explode
(
' '
, trim(
$sentence
));
echo
"The first word of string is: "
.
$arr
[0];
?>
Output:
The first word of string is: Welcome
Method 3: Using strstr() Function: The strstr() function is used to search the first occurrence of a string inside another string. This function is case-sensitive.
Syntax:
strstr( $string, $search, $before )
Program:
<?php
$sentence
=
'Welcome to GeeksforGeeks'
;
echo
"The first word of string is: "
.
strstr
(
$sentence
,
' '
, true);
?>
Output:
The first word of string is: Welcome
Method 4: Using preg_match() Function: This function searches string for pattern, returns true if pattern exists, otherwise returns false. Usually search starts from beginning of subject string. The optional parameter offset is used to specify the position from where to start the search.
Syntax:
int preg_match( $pattern, $input, $matches, $flags, $offset )
Program:
<?php
$sentence
=
'Welcome to GeeksforGeeks'
;
preg_match(
'/bw+b/i'
,
$sentence
,
$result
);
echo
"The first word of string is: "
.
$result
[0];
?>
Output:
The first word of string is: Welcome