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
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.
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)
This sounds more like a job for preg_match.
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