I want to throw validation error if –
- user input contains php tags
<?php(also for short tags<?) - but I want to allow XML tags (
<?xml) if there are no php tags detected. - In the presence of both, I would like to throw error.
I have been upto the following regex now –
`(.*?)<\?(php)?[^xml](.*?)`
But it does not match if the input contains only PHP short tags.
I want to match the following –
PHP Short tag
<?
PHP tag
<?php
PHP tag in the presence of xml tag
<?php ?>
<?xml
Do not match the following
<?xml
Update
Using this regex (.*?)<\?[^x](.*?) does not detect <? charater if the input contains only <?, without any ending whitespace. I guess I just need to add a condition in the following position to check further only if more characters are there –
|
v
`(.*?)<\?[^x](.*?)`
If I try with this – (.*?)<\?([^x]?)(.*?), it no more excludes the xml. Do you see where I am hanging? I guess there is something which regex allows here. Any ideas?
Do you need regex at all? Why not just do:
EDIT as mentioned in comments this won’t cover a file that contains
<?and<?xml(although you could count the number of occurences of<?and if this is GREATER than the number of<?xml, then throw an error).Re regex, you have a few options. If your regex flavour supports negative lookaheads, you can look for a
<?that isn’t followed by a ‘x’:or if it doesn’t support negative lookaheads, you could try
Either of these look for the presence of a PHP flag.