0

I am searching through HTML for "myfunc". However, the capitalization is uncertain. Unfortunately, PHP works with XPath 1 so all searches are case-sensitive.

How can I update the following code to search for "myfunc", "MyFunc", "myFunc", etc.?

foreach($domxpath->query('//*[@*[contains(.,"myfunc")]]') as $node) {

The following doesn't work. Nor did my attempt at implementing a translate() work-around.

foreach($domxpath->query('//*[@*[matches(.,"[mM][yY][fF][uU][nN][cC]")]]') as $node) {

If regex is not possible, how could I search on an array of strings (i.e. "myfunc", "MyFunc", "myFunc", etc.)?

Community
  • 1
  • 1
citizen
  • 353
  • 3
  • 13

2 Answers2

3

matches is a XPath 2 function, in XPath 1 you have to use translate to convert it to a normalized case:

 //*[@*[contains(translate(., "MYFUNC", "myfunc"),"myfunc")]]
BeniBela
  • 16,412
  • 4
  • 45
  • 52
0

Use the case-insentitive option for matches:

foreach($domxpath->query('//*[@*[matches(.,"myfunc","i")]]') as $node) {

To compare a sequence of values, just use = to do a node set comparison:

(. = ("myfunc", "MyFunc", "myFunc"))

will return true if anything in the left sequence anything in the right sequence. From XPath spec:

If both objects to be compared are node-sets, then the comparison will be true if and only if there is a node in the first node-set and a node in the second node-set such that the result of performing the comparison on the string-values of the two nodes is true.

wst
  • 11,681
  • 1
  • 24
  • 39