0

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

bluedaniel
  • 2,079
  • 5
  • 31
  • 49
  • What about "sultan", given your example with the word "sultana"? More generally, what about words that are both words with and without the trailing "a"? – Mike A. Oct 07 '09 at 11:30
  • so `item onea sultana is an item` would check the end letter of the word `sultana` and see that `sultana` is in the array and not recplace, although `onea` would not be in the array so it would be replaced. It would need to check every word ENDING in `a` – bluedaniel Oct 07 '09 at 11:38
  • So, you can guarantee that you would not want to split the line on the word "sultan"? Because that would be missed if we always ignored the word "sultana". Same with "are" vs. "area" and others. I guess my point is the letter "a" isn't a very good separator, if you have a choice in the matter. – Mike A. Oct 07 '09 at 11:45
  • Unfortunatley there is no choice, I do understand but chances are that its highly unlikely that it would end in these 'risky words' – bluedaniel Oct 07 '09 at 11:53

4 Answers4

1

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.
  • 398
  • 3
  • 12
  • that is doing something but its not combining the strings together, so try testing `100ml cream
    100g sugar
    650g cream cheesea zest of a lemona drop of vanilla extract` obviously it should echo two more `
    ` tags cheese
    a and lemon
    a
    – bluedaniel Oct 07 '09 at 12:30
  • 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
    s, you can do an echo join("
    ", $finalStrings);
    – Mike A. Oct 07 '09 at 12:54
  • 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 Oct 07 '09 at 13:07
  • 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. Oct 07 '09 at 13:20
0

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
  • 14,808
  • 4
  • 33
  • 50
  • Your code won't change the array. However, a small change will: foreach($words as $key => $word) { $words[$key] = preg_replace('/a$/', '', $word); } – Duroth Oct 07 '09 at 12:32
  • Thanks Duroth. As I said in my last edit, it late and my brain may not be working at peak efficiency. – pavium Oct 07 '09 at 13:14
0

This sounds more like a job for preg_match.

jldupont
  • 93,734
  • 56
  • 203
  • 318
0

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
  • 166,582
  • 36
  • 299
  • 288