I have written this JavaScript that calculate words, but I am unable to convert this JavaScript code to PHP regular expression. Can anyone help me with following code?
return str.replace(/\&\w+;/ig,"")
.replace(/lt;/g, "<")
.replace(/gt;/g, ">")
.replace(/(<([^>]+)>)/ig,"")
.replace(/^[^A-Za-z0-9-\']+/gi, "")
.replace(/[^A-Za-z0-9-\']+/gi, " ")
.split(" ")
.length - 1;
PHP does not have a
gflag, the closest equivalent is using/iswith the replace$limitset tonull(all).After that just explode it by ” “.
e.g.
It’s pretty late and I haven’t double-checked this. Hope it helps.
Edit: please be careful with escaping the backslash and single-quote.