views:

92

answers:

4

Ive got a string that are words bunched together and I need to seperate them, and every word that ends in 'A' should probably be on a new line,

item onea second itema third

I also need to check if the word ending in 'A' should actually end in 'A' like extra or sultana.

item oneasecond itemand an extra item

I have an array full of words ending in 'A' from this website http://www.morewords.com/ends-with/a so I just need the preg_replace function.

I really am learning everytime someone answers a question here so again thanks for everyones time and patience

A: 

Consider that it may be useful to explode() the string in order to separate it into an array of words:

$words = explode(' ', $string);

If they're separated by spaces.

Then you could loop through the array $words and check each one for a final 'a', rtrimming it if necessary.

preg_replace() is not always going to be the answer to your text manipulation needs.

EDIT: If you were to use preg_replace for each element of $words then

foreach ($words as $word) {
    $word = preg_replace('/(\w)a$/', '\1', $word);
}

Note, I haven't tried this, and I don't recall right now if this actually changes the array, but I think the regular expression should be about right. The important concept being a$, that is, a letter a at the end of the one-word string. I think that's the right syntax for replacing a letter (\w) followed by an 'a' at the end of a string with just the letter but it's very late here and my brain may not be working.

Plus, we're not taking into account your list of about 2900 words ending in 'a' (some of which I've never even heard of)

pavium
Your code won't change the array. However, a small change will: foreach($words as $key => $word) {$words[$key] = preg_replace('/a$/', '', $word);}
Duroth
Thanks Duroth. As I said in my last edit, it late and my brain may not be working at peak efficiency.
pavium
A: 

This sounds more like a job for *preg_match*.

jldupont
A: 

Not sure what you mean by 'and every word that ends in 'A' should probably be on a new line'. Always helpful if you post actual output besides the input-string.

You mean that a word ending with an 'a' should be followed by a new line (1)? Or that a word ending with an 'a' should have a new line before it? Or perhaps a combination of the two, making a word ending with an 'a' be placed on their own line (a line break placed before and after the word)?

$words = "item onea second itema third";
print_r(preg_split("/\s+(?=\S+a)/i", $words));           // 1
print_r(preg_split("/(?<=a)\s+/i", $words));             // 2
print_r(preg_split("/(?<=a)\s+|\s+(?=\S+a)/i", $words)); // 3
Bart Kiers
+1  A: 

You could do something like this:

// assoc array keyed on words that end with A
$endsWithA = array("sultana" => 1, ...); 

$words = split(' ', $string);

$newString = '';
$finalStrings = array();

foreach ($words AS $w) {    
    // if it ends with a, check to see if it's a real word.
    // if so, end the current string and store it
    if (preg_match("/a$/", $w) && !$endsWithA[$w]) {
        $w = preg_replace("/a$/","", $w);
        $newString .= $w;
        $finalStrings[] = $newString;
        $newString = '';
    }
    else {
        $newString .= $w . ' ';
    }    
}

// Get any remaining newString
if ($newString) $finalStrings[] = trim($newString);

print_r($finalStrings);

Haven't tested it, etc., but it would give you an array $finalStrings populated with the strings split from the original.

Update: fixed a couple of typos in the code.

Mike A.
that is doing something but its not combining the strings together, so try testing `100ml cream<br/>100g sugar<br/>650g cream cheesea zest of a lemona drop of vanilla extract` obviously it should echo two more `<br/>` tags cheese<br/>a and lemon<br/>a
bluedaniel
Ok, actually tested the code and fixed a couple of typos. Should work properly on your test string, at least. If you want to echo it out with <br />s, you can do an echo join("<br />", $finalStrings);
Mike A.
so close!! I changed `$newString = 'a ';` so the a appears back in the string, the only thing is that it picked up the a in `zest of a lemon`, obviously if its on its own like that it should be part of the $endsWithA but ive tried just putting in a space and it doesnt work. ideas?
bluedaniel
Looking at http://www.morewords.com/ends-with/a, it doesn't include the word "a" as a word that ends with "a". Maybe that's causing the problem? Is not in $endsWithA? I manually added it to my test and it seemed to work fine.
Mike A.
A tricky question and a great answer, thanks mike!
bluedaniel