0

How do you select the content of a string based on a changing count?Each time the loop is run the count increments by 1 and the next portion of the string is required.

$mystring = 'This is my string. This string is a sample. This is a problem';

So if $i==1 then I want

echo $newstring // This is my string.

At $i==2 I want

echo $newstring // This string is a sample.

At $i==3 I want

   echo $newstring // This is a problem.

I have looked at lots of reference pages on explode, substr, array_pop etc but I haven't seen a method that allows for the position of the trigger word to change based on an incrementing counter.

Steve Price
  • 600
  • 10
  • 28
  • Iteration usually starts at `0` in PHP. Splitting your input as others have answered is the right way. Also, if your words to split on are all single characters, try `strtok()`. – Walf Oct 12 '16 at 14:41
  • You may get better answers if your question had a real example (need this - tried this). Solution might go different way than your assumed algorithm. In you comment below I see that your string comes from html and "the word" is actually a `class` attribute in it. There are parsers for that. – shudder Oct 12 '16 at 15:52
  • @shudder The string is actually a db entry for shipping methods. All the html content is superfluous in the application that I'm looking at. I merely want "Free Shipping", Economy Delivery (1Kg) etc from whatever was originally stored, but must be able to get them in a given order. – Steve Price Oct 12 '16 at 15:56

3 Answers3

2

This could be answered with Explode a paragraph into sentences in PHP

foreach (preg_split('/[.?!]/',$mystring) as $sentence) {
    echo $sentence;
}

Also you can access each element:

$matches = preg_split('/[.?!]/',$mystring);
echo $matches[0]; // This is my string
echo $matches[1]; // This string is a sample
echo $matches[2]; // This is a problem
Community
  • 1
  • 1
pablopunk
  • 371
  • 1
  • 11
  • Can you explain why your example doesn't work if $mystring = 'Shipping (

    Free Shipping
    1 x IARP IA313200 Door Gasket

    Economy Delivery (1Kg)
    1 x WIP69457. Whirlpool Part Number 481946669457

    )'; $chr = 'AdvancedShipperShippingMethod">'; and I change '/[.?!]/' to '/[AdvancedShipperShippingMethod?!]/' echo $matches gives nothing
    – Steve Price Oct 12 '16 at 15:29
  • Can you tell me what is the output you want for that string? – pablopunk Oct 13 '16 at 13:34
  • In the example string I would want to find $matches[0]; //Free Shipping and $matches[1]; //Economy Delivery (1Kg) – Steve Price Oct 13 '16 at 15:47
  • Oh ok, then you need to parse it as html, do you think [this answer](http://stackoverflow.com/questions/6083076/php-way-of-parsing-html-string) would work? – pablopunk Oct 13 '16 at 16:55
0

If . is the part where you want to explode the string then you can use regular expression.

$line = 'This is my string. This string is a sample. This is a problem.';
preg_match("/([[:alpha:]|\s]+\.)/i", $line, $match);

echo $match[1]; 

Example https://regex101.com/r/4SHAJj/1

Suraj
  • 2,181
  • 2
  • 17
  • 25
  • The string was a pure sample. What if you wanted to explode on a specific word? – Steve Price Oct 12 '16 at 14:18
  • Yes you can do that. Actually regular expression are pretty handy in this kind of things. Just to show you what can we do with regular expression. In this sentences we removed the country codes from the sentences. https://regex101.com/r/w6HUsQ/1 – Suraj Oct 12 '16 at 14:23
  • It's like black magic to me! I get confused by all the \ and /, let alone the content within [ ]. If I wanted to search for all occurrences of AdvancedShipperShippingMethod and be able to access them using $match[1], $match[2] etc how would I format the regex? – Steve Price Oct 12 '16 at 14:34
0

I found a solution to this, and although it may not be the cleanest or best way, it does work. $shippingData contains

Shipping (<div class="AdvancedShipperShippingMethodCombination"><p class="AdvancedShipperShippingMethod">Free Shipping <br />1 x IARP IA313200 Door Gasket</p> <p class="AdvancedShipperShippingMethod">Economy Delivery (1Kg) <br />1 x WIP69457. Whirlpool Part Number 481946669457</p></div>)';

Code used:

$shippingData = $order_result->fields['shipping_method'];
$matches = preg_split("/(AdvancedShipperShippingMethod\">)/", $shippingData);
$method = $matches[$n]; //$n is a count that increments with while/next loop
$method = substr($method, 0, strpos($method, "<br />"));
$method = "Shipping (".$method.")";
}
Steve Price
  • 600
  • 10
  • 28