how to get everything before and after a certain word in string [closed]
Asked Answered
S

6

5

how do I get everything in a php string before and after a certain word? For example, if I have a string that says "5 times 8", how do I extract the 5 and the 8 and store them into separate variables? I'm kinda new to PHP.

I've tried

$foo = "5 times 8";
$foo = str_replace(" times ","",$foo);

Then what? The result is "58"

Sexagenarian answered 25/3, 2013 at 0:22 Comment(5)
php.net/manual/en/function.substr.php look here :-)Sybarite
Nor have you given enough detail about the question. For example, what happens if your subject was "5 times times 8". Which instance of "times" is your "certain word"?Measured
I didnt tell you what I've tried because I am new to PHP and didnt know where to startSexagenarian
also, I will only have one instance of times in my stringSexagenarian
@ThomasLai - In that case, you need to put forth some actual effort before asking a question at Stack Overflow. We are not your personal code monkeys.Rennin
B
21

If you are certain that your input string contains " times " then you can do this:

$input = "5 times 8";
list($a, $b) = explode(' times ', $input);

echo $a; // "5"
echo $b; // "8"

If you need to do something a little more complex, use regular expressions:

$input = "5 times 8";
if (preg_match('!(\d+)\s*times\s*(\d+)!i', $input, $m)){
    $a = $m[1];
    $b = $m[2];
}
Blearyeyed answered 25/3, 2013 at 0:25 Comment(1)
+1. But @ThomasLai be aware that the regex specified here works explicitly for numbers either side of your "word" (as per your one example)... so if you need eg text either side of your word, you'd need to replace Cal's \d's with some other character class - refer regular-expressions.info/reference.htmlMeasured
C
5

You can use the explode() function, like this:

$string = "5 times 8";
$var = explode(' times ', $string);
echo $var[0]; //echoes 5
echo $var[1]; //echoes 8
Chalybite answered 25/3, 2013 at 0:25 Comment(0)
U
3

you can explode() it using the your string...

$string = "5 times 8";
$var = explode(' times ', $string);
echo $var[0]; //echoes 5
echo $var[1]; //echoes 8

This will work

Ultimatum answered 25/3, 2013 at 0:28 Comment(0)
M
1

You've said in your question "how to get everything before and after a certain word". So this will get everything. Replace the instances of .* with eg \d* to get eg numberic digits either side.

The following will return 0 if no match found at all, or >0 if matches found.

preg_match_all('/^(.*)times(.*)$/', $yourStringToSearch, $arr, PREG_PATTERN_ORDER);

If matches found, $arr will be populated.

So eg if "times" is found, but without pre/post text, then it'll return 1, and $arr[0] will contain only "times".

So for example, if $yourStringToSearch contains "5 times 8", then $arr will be populated as follows (where $arr[0] is the entire match, $arr[1] is the pre-text, and $arr[2] is the post-text).

Array
(
    [0] => Array
        (
            [0] => 5 times 8
        )

    [1] => Array
        (
            [0] => 5
        )

    [2] => Array
        (
            [0] =>  8
        )

)

But, be careful because if $yourStringToSearch = "5 _times_ -times- 8" it'll return:

Array
(
    [0] => Array
        (
            [0] => 5 _times_ -times- 8
        )

    [1] => Array
        (
            [0] => 5 _times_ -
        )

    [2] => Array
        (
            [0] => - 8
        )

)

That is - you need to either specify what you want to happen in the case where your search string appears more than once in the target, or accept the "default" in this case where the first match on your search term is "the" match.

If you need to ensure the search term is surrounded by spaces, then the following two will work. THe first assumes you don't want to "capture" the space, while the second will include the space in the captures:

preg_match_all('/^(.*) times (.*)$/', $yourStringToSearch, $arr, PREG_PATTERN_ORDER);

preg_match_all('/^(.* )times( .*)$/', $yourStringToSearch, $arr, PREG_PATTERN_ORDER);

Measured answered 25/3, 2013 at 0:26 Comment(2)
Your code does not return what you think it does. $arr[1][0] contains "5 times". update: your new code does indeed return what you claim :)Blearyeyed
@Blearyeyed sorry copied a boundary case test (to highlight a problem with the question!) into my answer accidentally. Fixed.Measured
K
1

I'm not fully sure of what you're trying, but maybe this can help:

If you explode the string with explode($delimiter, $string, [$limit]), being in the case you had as an example "times" as the $delimiter, you can get an array with 5 and 8 as values.

Therefore:

$string = "5 times 8"
$delimiter = " times "
$array = explode($delimiter, $string)

would yield

$array[0] = 5
$array[1] = 8

Hope this helps!

Kew answered 25/3, 2013 at 0:29 Comment(1)
Your code doesnt contains any semicolonsWall
P
-1

You can use the substr function : http://www.php.net/manual/en/function.substr.php and if you don't know the position of the word, use strpos to get it : http://php.net/manual/en/function.strpos.php

Propitious answered 25/3, 2013 at 0:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.