Match string with php dom xpath by first and last character -
i want match html string(s) starting % , ending % character searching text xpath.
conditions:
- string starts %
- string ends %
- can have none or multiple matches within 1 html document
- string can contain (but optinional) :: needs @ least 1 alphabetic character before , after ::
- between first , last % alphabetic, numeric , - characters allowed.
the best got $xpath->query("//*[text()[starts-with(., '%')][substring(., string-length(.) - 1) = '%']]");
but not working. new php dom stuff , finding hard answers on own. explanations valued!
thanks in advance!
edit
see comments below in case use of preg_match_all beter. i'm using following code this:
preg_match_all('/%{1}[a-za-z0-9-]+?(::?[a-za-z0-9-]+?)?%{1}/', $string, $match);
improvements pattern accepted.
this isn't xpath's strong point - you're describing best handled regexp engine (which, in php, presumably mean iterating on nodes , running each 1 through preg_match
).
nonetheless, here's (very) hacky xpath approach think want. can see working demo @ this xmlplayground.
root/node[ substring(., 1, 1) = '%' , substring(., string-length(.)) = '%' , not(string-length(translate(substring(., 2, string-length(.)-2), 'abcdefghijklmnopqrstuvwxyz0123456789-:', ''))) , ( ( contains(., '::') , substring(., 2, 1) != ':' , substring(., string-length(.)-2, 1) != ':' ) or not(contains(., '::')) ) ]
Comments
Post a Comment