EllisLab text mark
Advanced Search
     
Test if field contains string?
Posted: 26 June 2008 09:47 PM
Avatar
Joined: 2005-08-31
654 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”

 
Posted: 27 June 2008 02:15 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2006-04-23
484 posts

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

 Signature 

OLD username was jammo, NEW username is OrganizedFellow

It’s a struggle even to keep focused. This is the best of my AD/HD & GTD.
The exceptionally slow growth of my web dev projects is eclipsed by my patience, understanding and desire to learn AS MUCH AS POSSIBLE as I slowly progress.

 
Posted: 27 June 2008 02:43 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2005-08-31
654 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.

 
Posted: 27 June 2008 02:43 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2004-05-15
29075 posts

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

 
Posted: 27 June 2008 02:53 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2005-08-31
654 posts

which would be…...?

 
Posted: 27 June 2008 03:11 AM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2004-05-15
29075 posts

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

 
Posted: 08 August 2008 02:10 AM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2004-02-27
167 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}"07); // first 7 letters, starting at zero
if ('http://' == $test) echo "Visit:";
if (
'http://' != $test) echo "Download:";
?> 
 
Posted: 27 April 2009 01:36 PM   [ # 7 ]   [ Rating: 0 ]
Joined: 2008-03-21
69 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)?

 
Posted: 27 April 2009 02:27 PM   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2006-04-15
12649 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

 Signature 

Shopping Cart Plugin | Full List Of Add-Ons | About Me
——————————————————————————————
2.x Bug Tracker | Upgrade Errors

 
Posted: 11 May 2010 05:52 PM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2007-08-14
234 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.

 Signature 

Jim Pannell | Six Media Web Development | Copenhagen, Denmark
Twitter: @jimpannell

 
Posted: 11 May 2010 07:33 PM   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-03
326 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.

 Signature 

http://robsanchez.com
http://twitter.com/_rsan
http://github.com/rsanchez

 
Posted: 11 May 2010 07:35 PM   [ # 11 ]   [ Rating: 0 ]
Joined: 2006-10-26
39 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"}
                       
<p>the string 'abc' is contained in the field</p>
{/if} 
 
Posted: 12 May 2010 05:11 AM   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2007-08-14
234 posts

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

 Signature 

Jim Pannell | Six Media Web Development | Copenhagen, Denmark
Twitter: @jimpannell

 
Posted: 12 May 2010 09:28 AM   [ # 13 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-03
326 posts

You’re welcome smile

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

 Signature 

http://robsanchez.com
http://twitter.com/_rsan
http://github.com/rsanchez