Home > Software engineering >  Calling str_contains or any PHP function in XSLT to match a string to a pattern
Calling str_contains or any PHP function in XSLT to match a string to a pattern

Time:12-03

I am trying to use a PHP function to see if a comment in a XSLT contains "Written by."

I have not had any luck with str_contains:

<!-- in a loop -->
<xsl:choose>
  <xsl:when test="php:functionString('str_contains', 'Written by', comment)">
       <xsl:value-of select="comment"/>
  </xsl:when>
</xsl:choose>

My understanding is the function goes: (PHP Function, Pattern to Match, Variable) but it is not working (the condition is not true when the data is matched).

I am wondering:

  1. Is it a version problem? str_contains must be PHP 8 but maybe the server is 7. Not sure.
  2. Is it a syntax problem?
  3. If it is a version problem, what is a different function to see if a comments begins with or contains that pattern to match?

CodePudding user response:

the condition is not true when the data is matched

If the function is returning a wrong result then you're probably not using it correctly. If it were a version problem (i.e. function not supported), I would expect an error message.

what is a different function to see if a comments begins with or contains that pattern

XPath has native functions for both - and IMHO they should be your first choice when using XSLT, before you reach for extension functions:

  • Related