If string contains

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

I need to figure out if a string contains " and if it does, I need to execute something.

Code would look something like this, I don't know PHP very well though.

{
if ($string contains '"')
{
bg = '$string'
}

else
{
bg = "$string"
}
}

Is that possible in php?

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

I found it, but I just found out it won't work for my problem Mad

<?php
  $string
= 'Hello World!';
  if(
stristr($string, 'He') === FALSE) {
   echo
'"earth" not found in string';
  }
 
  else
  {
  echo
"It does contain He";
  }
?>

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

Why won't it work for your problem?

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

It is really hard to explain Cpt. But inside the CMS I am using, when ever you type {PAGETITLE} anywhere in the code, the title of the page is displayed. So if I set the $string to equal {PAGETITLE} $string = "{PAGETITLE}" what happens if the page title has a " in it? The code screws up. Is there a way to define a string without " or ' ?

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

Basically I think the problem is your methodology. You're trying to have the CMS write php code, and that just doesn't work, what you should do, if anything is alter the CMS so that it does an addslashes to whatever is replacing {PAGETITLE} (if you want to do it at all, which again, I don't reccommend. The even better plan is to find out what where PAGETITLE is coming from and use that variable for the $bg, you might have to make it global, but at least it will work.

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Quote: Basically I think the problem is your methodology. You're trying to have the CMS write php code, and that just doesn't work, what you should do, if anything is alter the CMS so that it does an addslashes to whatever is replacing {PAGETITLE} (if you want to do it at all, which again, I don't reccommend. The even better plan is to find out what where PAGETITLE is coming from and use that variable for the $bg, you might have to make it global, but at least it will work.

Did exactly that Cpt. It finally works. Thank you soooo much. Smiling I was working on that stupid problem for two weeks.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.