0

I've already tried a couple of regex but cannot fix this one:

I need an enter line before the hashtags start (so there is some space).

Example:

It's essential to keep your pup cool in the summer months! Try to keep them out of the heat as much as possible - keep the house cool, provide shade and run a fan for them if you can. #Dogs #Animals #Summer #DogCare #Humidity #HeatStroke #Cooling #HeatExhaustion #StayCool #HealthyPups

Should be:

It's essential to keep your pup cool in the summer months! Try to keep them out of the heat as much as possible - keep the house cool, provide shade and run a fan for them if you can.

#Dogs #Animals #Summer #DogCare #Humidity #HeatStroke #Cooling #HeatExhaustion #StayCool #HealthyPups

If there is a hashtag in the text it should not add whitespace, for example (note hashtags in text!):

It's essential to keep your pup cool in the summer months! Try to keep them out of the heat as much as #possible - keep the house cool, #provide shade and run a #fan for them if #you can. #Dogs #Animals #Summer #DogCare #Humidity #HeatStroke #Cooling #HeatExhaustion #StayCool #HealthyPups

How can I solve this with Regex and PHP?

7
  • So if I said "I love pizza! #pizza #pepperoni #PizzaPizza Did I mention I love pizza?" - there should not be a break because there's additional non-hash stuff at the end? Commented May 31, 2023 at 18:40
  • What's the objective difference between the two cases? The hashtags are always "in the text". Commented May 31, 2023 at 18:46
  • Yes correct! Chris Commented May 31, 2023 at 18:46
  • Only the hashtag group at the end should have space inbetween. Commented May 31, 2023 at 18:46
  • So if the text ends with a series of hashtags, you want a newline before the first one? Commented May 31, 2023 at 18:47

2 Answers 2

2

You can match a series of hashtags with the regexp:

(?:#\w+\s*)+$
  • #\w+ matches a single hashtag
  • \s* matches the optional whitespace between the hashtags
  • + after the group matches a sequence of at least one of these
  • $ matches the end of the input string.

You can then use preg_replace() to insert a blank line before this.

$text = preg_replace('/((?:#\w+\s*)+)$/', "\n\n$1", $text);
Sign up to request clarification or add additional context in comments.

Comments

1

Match a space which is followed by one or more sequences of hashtags until the end of the string. Inform preg_replace() to make only 1 replacement by adding 1 as the fourth parameter.

Code: (Demo)

echo preg_replace('/ (?=(?:#[a-z]+\s*)+$)/i', "\n\n",  $text, 1);

Or without a lookahead or a replacement limit: (Demo)

echo preg_replace('/ ((?:#[a-z]+\s*)+)$/i', "\n\n$1",  $text);

Both approaches will cleanly replace the space before the sequence of hashtags with the two newlines.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.