Question

I have this code:

if(!strstr($value, '<p>')){...}

This outputs data from a database to an excel file excluding any fields that contain <p>.

I would also like to exclude fields that contains </script>

Ive tried these two code snippets without success:

if((!strstr($value, '<p>')) and (!strstr($value, '</script>')))

And:

if(!strstr($value, '<p>', '</script>'))

Basically my question is how can I exclude both <p> and </scrpit> from my excel file?

Was it helpful?

Solution

You can use regural expression and preg_match function for this one:

if(!preg_match("/(<p>|<\/script>)/", $value)) {
    // Do your magic!   
}

You can read more about preg_match here: PHP preg_match And if you have problems with regular expressions, check out this example: example and play around with it to get a hold of regular expressions.

And as far as I can see, your logic should work just fine. Maybe your $value variables hold wrong data. Anyway, you have something to get you going.

Hope this helps!

OTHER TIPS

this works.

<?php

$a = array('ok0', 'somthing <p>','2222 </script>', 'ok1');
foreach ($a as $value)
    if((!strstr($value, '<p>')) && (!strstr($value, '</script>')))
        echo $value;

I'd suggest using strpos instead, but this should work fine:

if(!strstr($value, '<p>') and !strstr($value, '</script>')){
    // Write your file
}

...if it doesn't, something else is going on. Your $value may not be what you expect, for example.

EDIT Replaced && with and per op's code example

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top