I want to build a pattern to exchange integer numbers which are the only thing on the line
If there is a word on the same line as the integer I do not want to change anything.
I tried $pattern = '/(.)[0-9][0-9](.)/';
but this doesn’t work well for me
and when I try for example $pattern = '/1(.)2(.)3(.)/'; it will replace the only single numbers which I will put in pattern
I want to replace example subject of this
subject = "
1
2
3
4
5
6
7
8
9
10
"
the numbers must be integer not decimal and amouth of them are random but there musn’t be any text on the same line as the number?
Any ideas?
What you need is multi-line mode
m. If you use it^matches at the beginning and$matches at the end of each line. Then use\d+for an arbitrarily long integer number:The
[ \t]*allow an arbitrary number of spaces and tabs in addition to the number. Note that this will remove the whitespace along with the number. If you want to keep the whitespace, useAnd change your replacement string to
EDIT:
I realize now that you don’t have line breaks, as in
\ror\nor\r\nbut<br>tags. That makes it a bit more difficult. Something like this should cover most cases:Again, you need to add
$1and$2around your replacement string to not remove the<br>tags.But as Andy said, HTML should not be dealt with using regular expressions. Either use the built-in DOM module provided by PHP, or some 3rd party library like this one.