ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Test if field contains string?

June 26, 2008 9:47pm

Subscribe [9]
  • #1 / Jun 26, 2008 9:47pm

    james Brown's avatar

    james Brown

    492 posts

    How can I do a conditional to test whether a field contains a specified string? I know that you can do a {if custom_field=="string"}, but I believe that tests for exact matches. What if I want to test if any part of the field contains the string. So that these would all prove true:
    “string words words words”
    “words words string”
    “wordstring words words words”

  • #2 / Jun 27, 2008 2:15am

    OrganizedFellow's avatar

    OrganizedFellow

    435 posts

    Why not use a client-side validation?
    If I understand your situation correctly, why not jQuery?

  • #3 / Jun 27, 2008 2:43am

    james Brown's avatar

    james Brown

    492 posts

    yes, i think you are misunderstanding. I am not looking for validation of an input field. This is for a display template for a weblog. The basic structure is:

    {if custom_field CONTAINS "String"}
    display this
    {if:else}
    display that
    {/if}

    Using the standard equality comparison operator, it will only evaluate to true if the comparison string is exactly the same as the field value…not if the comparison string is contained somewhere within the string.

  • #4 / Jun 27, 2008 2:43am

    Ingmar's avatar

    Ingmar

    29245 posts

    You need regular expression pattern matches. Or, put differently, you need PHP.

  • #5 / Jun 27, 2008 2:53am

    james Brown's avatar

    james Brown

    492 posts

    which would be…...?

  • #6 / Jun 27, 2008 3:11am

    Ingmar's avatar

    Ingmar

    29245 posts

    You will have to compare the two strings somehow. I suppose ereg(), or perhaps substr(), might be up to the job.

  • #7 / Aug 08, 2008 2:10am

    Oxygen Smith's avatar

    Oxygen Smith

    120 posts

    I dunno if this was solved, but I’m learning some basic PHP, so if anyone’s looking for how to do this, substr works great.

    Enable PHP for output, then make a comparison like this. This tests to see if a field (document_url_en) contains a URL offsite (denoted by beginning with http://) or a reference to a file onsite for downloading (anything but http://) :

    <?php
    $test = substr("{document_url_en}", 0, 7); // first 7 letters, starting at zero
    if ('http://' == $test) echo "Visit:";
    if ('http://' != $test) echo "Download:";
    ?>
  • #8 / Apr 27, 2009 1:36pm

    gcylinder

    49 posts

    This is exactly what I was looking for. Could you put this snippet of PHP code inside Expression Engine code (i.e., nest it inside EE weblog tags)?

  • #9 / Apr 27, 2009 2:27pm

    Mark Bowen's avatar

    Mark Bowen

    12637 posts

    There’s also a plugin called PHPStringFun which might help on this. I say might as I haven’t tried it out in this exact way but hopefully it might work. If not then it’s a nice little plugin anyway that might come in handy for people somewhere along the way.

    Best wishes,

    Mark

  • #10 / May 11, 2010 5:52pm

    Jim Pannell's avatar

    Jim Pannell

    187 posts

    Anyone know how I can do a conditional on whether a string include xyz using the PHPStringFun plugin? I usually do it in raw php, but would love to be able to use a EE plugin instead.

  • #11 / May 11, 2010 7:33pm

    Rob Sanchez's avatar

    Rob Sanchez

    335 posts

    Jim- I made this plugin a while back: http://barrettnewton.com/site/products/bn_regex/

    It gives you access to preg_match, which you can use to check if a string is within another string:

    {exp:weblog:entries weblog="blog"}
    {if {exp:bn_regex:preg_match pattern="/xyz/" subject="{custom_field}"}}
    There was a match.
    {/if}
    {/exp:weblog:entries}

    It uses regex patterns, so don’t forget the trailing and leading slashes.

  • #12 / May 11, 2010 7:35pm

    jtoast

    32 posts

    In answer to the original James Brown question, I came up with this, with a little help from the above-mentioned PHPStringFun plugin (thanks!) and the following thread which details how to use the output of the PHPStringFun (also, thanks!):

    http://ellislab.com/forums/viewthread/145229/

    I’m sure this is not the only way to determine whether a field contains a specified string (what JB was looking for above), but it worked well for me, using the substr_count function:

    {if "{exp:phpstringfun function="substr_count" par1="abc"}{field_name}{/exp:phpstringfun}" == "1"}
                           the string 'abc' is contained in the field
    {/if}
  • #13 / May 12, 2010 5:11am

    Jim Pannell's avatar

    Jim Pannell

    187 posts

    Hey Rob - that’s EXACTLY what I needed and is working a treat for me - many, many thanks.

  • #14 / May 12, 2010 9:28am

    Rob Sanchez's avatar

    Rob Sanchez

    335 posts

    You’re welcome 😊

    And, it does a whole lot more than just the example I showed here. Check out the plugin usage for more info.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases