-2

I want to ask about is it possible to get some string that between a specifi keyword? For example, I have 2 sentence like these:

I will go to #new bathroom and wash the car#

Result: bathroom and wash the car

Someone need an #new icebreaker# to hold that problem

Result : icebreaker

I want to make condition to get all words between the #new # Any idea how to create this?

My code so far:

<?php

$sentence = "I will go to #new bathroom and wash the car#";
$start = strpos($sentence, "#new");
//$end = strpos($sentence, "#");
$end = 20; //because my strpos still wrong, I define a static number
$new = substr($sentence, $start, $end);
echo $new;

?>

My problem is I can't find a way to chase the last hashtag

Cross Vander
  • 2,077
  • 2
  • 19
  • 33

5 Answers5

1

Another way different to Regular Expression is to explode the string and replace the new in the sentence

This will just work if you have only one keyword in the sentence #new

$string = "I will go to #new bathroom and wash the car#";
$string1 = "Someone need an #new icebreaker# to hold that problem";

function getString($string, $delimiter = '#')
{
    $string_array = explode($delimiter, $string);
    return str_replace('new ', '', $string_array[1]);
}

echo getString($string);
//bathroom and wash the car

echo getString($string1);
//icebreaker

I'd like more work with arrays

$string = [
 "I will go to #new bathroom and wash the car#",
 "Someone need an #new icebreaker# to hold that problem"
 ];

function getString($string, $delimiter = '#')
{
    $result = [];
    foreach ($string as $value) {
        $string_array = strstr($value, $delimiter) ? explode($delimiter, $value) : [];
        $result[] = isset($string_array[1]) ? str_replace('new ', '', $string_array[1]) : NULL;
    }
    return $result;
}

print_r(getString($string));


/*
Array
(
    [0] => bathroom and wash the car
    [1] => icebreaker
)
*/
Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
  • Make sure there's a `$delimiter` in the `$string`, otherwise the `explode()` won't work and you'll get an undefined index notice. Also, when there's no `#new` it still gets what's between the two shebangs. Lastly, when there's an odd number of shebangs it gets messed up. – ishegg Sep 25 '17 at 14:13
  • @ishegg In the second snippet I provide a solution for the undefined index notice...about the odd number of hashtag...the OP said he has sentences...I was trying to provide other way different to regular experssion for the specific case he show up in the question – Emilio Gort Sep 25 '17 at 14:25
  • Of course, I agree that's the better way! Regular expressions should always be the last option, since they're more expensive. Though in this case, the other way seemed more intricate. – ishegg Sep 25 '17 at 14:26
  • Sorry to ask, but is this solution quicker/lighter than ishegg solution? Before using strpos, I try to break the sentence down to array by space delimiter but seems like too heavy for the program IMO – Cross Vander Sep 25 '17 at 14:34
  • Regular Expressions should be the last option like @ishegg said...I don't know if in this case it matter the heavy part, explode is always faster and lighter than REGEX, just be sure the conditions are met for your program, if you have more than one keyword in a single sentences, you could lost the other – Emilio Gort Sep 25 '17 at 14:45
  • 1
    @EmilioGort OK! thanks for answering, I will keep this answer too for next development. Thank you so much! – Cross Vander Sep 25 '17 at 14:58
1

I have written the following code for your problem but please bare in mind that i am still a beginner myself. It works exactly how you want it to but i am sure there are better solutions out there.

<?php

$string = "I will go to #new bathroom and wash the car#";
$stringArray = str_split($string);
$output = '';
$count = 0;

foreach($stringArray as $letter){

    if($count == 0 && $letter == '#'){
        $count = 1;
    } elseif($count == 1){
        if($letter != '#'){
            $output .= $letter;
        } else {
            $count = 2;
        }
    }

}

echo $output;

?>

hope this helps :)

Scully
  • 11
  • 1
1

Use this regular expression:

/#new (.+)#/i

Together with preg_match(), you'll get your match in an array:

<?php
$string = "Someone need an #new icebreaker# to hold that problem";
preg_match("/#new (.+)#/i", $string, $matches);
var_dump($matches[1]); // icebreaker

Demo

If you anticipate more than one possible match, use preg_match_all() to get them all.

ishegg
  • 9,685
  • 3
  • 16
  • 31
0

You can use regex to match that. here is a links and a simple regex.

(#)\w+(#)

(\#)+(.)+(\#)

https://regexr.com/

http://php.net/manual/en/function.preg-match.php

Community
  • 1
  • 1
Prav
  • 2,785
  • 1
  • 21
  • 30
0

You can search "#" from end, like $end = strpos($sentence, "#", -0);

and than get substring as you already have.

$new = substr($sentence, $start, $end);
Emilio Gort
  • 3,475
  • 3
  • 29
  • 44