preg_match_all words without html tags

qlithe

Supreme Member
Joined
Feb 14, 2012
Messages
1,252
Reaction score
288
Hey!

I have this piece of html:
HTML:
<p>This is a sentence with a <a href="http://google.com">link</a> inside</p>

And I want to use preg_match_all to get all the words except the html tags including the link. I've been trying to solve this for too long now so I decided to make a topic for some help. I want the result to look like this:

This
is
a
sentence
with
a
inside

This is the regex so far:
Code:
/(\w+)/

and I have tried with different methods like lookahead, lookbehind etc but I just cant get it to work. It currently includes
p
a
href
and part of the link and anchor
 
Php?
How about doing strip_tags(str)?
 
Php?
How about doing strip_tags(str)?

Yes problem is I want to do some functions on the words and keep it in the original format (with all the tags etc). Maybe its not much of a deal in this small example but for more complex situations it gets tricky..
 
Yes problem is I want to do some functions on the words and keep it in the original format (with all the tags etc). Maybe its not much of a deal in this small example but for more complex situations it gets tricky..
You will have even more problems if you use regex. The best way to handle complex html strings is by using the domdocument in php. If you have an input string and output string, provide me here in the thread. I will try solving it when I am online from a computer. :)

Ps.. strip_tags has a second parameter for keeping some tags.. Google it.
 
Lets say you have an article in html including normal tags like p, h2, a link somewhere etc. And you want to loop through all the words, even one char words, and run a function on them (like through preg_replace_callback or something similar). But you dont want to include the p, a, href etc

How could I achieve this without doing something unecessarily complex?
 
Lets say you have an article in html including normal tags like p, h2, a link somewhere etc. And you want to loop through all the words, even one char words, and run a function on them (like through preg_replace_callback or something similar). But you dont want to include the p, a, href etc

How could I achieve this without doing something unecessarily complex?
If I understood you correctly, that IS something complex. Are you trying to make a spinner or something similar? It can be done, but I don't think there's an easy way for this. Your best bait would be to raid github and find an open source module that already does the job.
 
A better solution would be to match all html tags from the string. Then you replace them with an empty string.

Then you're left with a sentence without html tags.

km6HcG.png


https://www.regextester.com/93515

Hope this helped :)
 
What do you mean? Same link as above
Grrrrr sorry. I totally pasted the wrong link :D

The regex I meant was

Code:
/(?<=>)[\w\s]+(?=<)/g
Sorry I am mobile right now, so I can't give you a test link. Please try the regex yourself.
 
Grrrrr sorry. I totally pasted the wrong link :D

The regex I meant was

Code:
/(?<=>)[\w\s]+(?=<)/g
Sorry I am mobile right now, so I can't give you a test link. Please try the regex yourself.

thanks for taking the time, not quite what im looking for but almost

i want to match every word and not just the space between the tags
 
thanks for taking the time, not quite what im looking for but almost

i want to match every word and not just the space between the tags
Oops okay. I wouldn't be in front of my computer before Monday evening. If you still do not find a solution, I will give you one on Monday. Till then, stay tuned and keep trying. ;)
 
Oops okay. I wouldn't be in front of my computer before Monday evening. If you still do not find a solution, I will give you one on Monday. Till then, stay tuned and keep trying. ;)
Umm nevermind @qlithe, my curiosity managed to get me online from a computer :D

What if you use this?

Code:
https://regex101.com/r/bZ1gT7/10
PHP code:
Code:
https://regex101.com/r/bZ1gT7/10/codegen?language=php
 
Umm nevermind @qlithe, my curiosity managed to get me online from a computer :D

What if you use this?

Code:
https://regex101.com/r/bZ1gT7/10
PHP code:
Code:
https://regex101.com/r/bZ1gT7/10/codegen?language=php

Hm not quite ..

What I'm looking for are matches like
match 1: MLA
match 2: Supplement
match 3: Test
etc

However the skip f thing was something new, gonna play around with it and see what i come up with
 
Back
Top