0

I am trying to find a string from a html section

Example

<div class="container" data-id="Youtube" style="height:100%;width:100%;border:none;">
<div class="inner"></div>

Generally we find a string using

if(strpos($string,"YouTube") !== false)

but for a sample test i am trying to store the html code into a php variable

$string="<div class="container" data-id="Youtube" style="height:100%;width:100%;border:none;">
    <div class="inner"></div>";

when i try to find for a youtube in $string , It always returns false(does not exist)

Can anyone help me to find the string in a html code Thank you

Ramaraju.d
  • 1,301
  • 6
  • 26
  • 46
  • 3
    youtube !== Youtube, use a case insensitive method (stripos in your example) – hank May 14 '13 at 13:57
  • 2
    You should probably surround your entire string in single quotes... – jonhopkins May 14 '13 at 13:58
  • Also, be careful with your quotes. You may want to open your variable with single quotes, so that you can use double quotes in the HTML – sbatson5 May 14 '13 at 13:58
  • I guess it's time for the [***"You can not parse HTML with regex"***](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) question again ! – adeneo May 14 '13 at 13:59
  • Now we will vote for any question, just randomly yea? what's wrong with the question? why -1? – Mr. Alien May 14 '13 at 14:01
  • Well I didn't downvote, but I am guessing that the fact the given "sample test" would immediately give a parse error *may* be why people do not think the OP has actually tried it – Anigel May 14 '13 at 14:03
  • @Anigel Yea but anyways this is far better than other, it has code, it is on point, it is formatted, downvote breaks their confidence – Mr. Alien May 14 '13 at 14:05
  • Parse error !! no i am testing on my localhost , No error found – Ramaraju.d May 14 '13 at 14:06
  • @Ramarajudantuluri Orly? php -r '$string="
    ' PHP Parse error: syntax error, unexpected T_STRING in Command line code on line 1
    – Anigel May 14 '13 at 14:08
  • And I am comparing Youtube!==Youtube but not youtube, i need exact match – Ramaraju.d May 14 '13 at 14:08
  • @Anigel Ohh my mistake. i have to include a ; and i would have wrapped the entire string in single quote. Sorry. But i tried the correct way on my machine – Ramaraju.d May 14 '13 at 14:12
  • 1
    This is where it gets confusing, if you had given the actual code you had tried on your machine then most of the people would not have wasted their time giving you comments and answers about a problem that only existed in the sample code you gave above, which was not the same as your actual code – Anigel May 14 '13 at 14:14
  • Ya, will take care before posting a question. I just intended to point out how i tried and not the syntax like missing semi-colon in my context. – Ramaraju.d May 14 '13 at 14:20

6 Answers6

3

try:

if(strpos($string,"Youtube") !== false)

if this is your problem use stripos() for case insensitivity.

Fabio
  • 23,183
  • 12
  • 55
  • 64
cerkiewny
  • 2,761
  • 18
  • 36
3

use single quote around string here

$string='<div class="container" data-id="Youtube" style="height:100%;width:100%;border:none;">
<div class="inner"></div>';

also use stripos for case-sensitivity

if(stripos($string,"YouTube") !== false)
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
1

Try to wrap your html with simple quotes :

$string = '<div class="container" data-id="Youtube" style="height:100%;width:100%;border:none;">
    <div class="inner"></div>';
Strategio
  • 428
  • 2
  • 9
1

Try with if(strpos(strtolower($string),"youtube") !== false)

I always use strtolower() to avoid similar problems.

V.Vachev
  • 341
  • 1
  • 7
  • 20
1

I made this simple test for you....it's working on my localhost:

<?php

    $string="<div class='container' data-id='Youtube' style='height:100%;width:100%;border:none;'><div class='inner'></div>";
    $search = "Youtube";
    if(strpos($string, $search) !== false)
    {
        echo "Found";
    }
    else
    {
        echo "Not found";
    }

    ?>
Hackerman
  • 12,139
  • 2
  • 34
  • 45
  • Ya, I tried this already. a user will enter this html code through a form, and it will be stored in database and used later.So i think i must mention the user that html attribute values must be single quoted. right? – Ramaraju.d May 14 '13 at 14:15
  • Yes @Rama ....your html should looks like my $string var...if you try this code, it's works....if you change the $search value to "Zebra", it echo "Not found" ... – Hackerman May 14 '13 at 14:18
1

You can use xpath + dom objects just like

class CeiXML extends SimpleXMLElement
{

    public static function asHTML($xml)
    {
        $ele = dom_import_simplexml($xml);
        $dom = new DOMDocument('1.0', 'utf-8');
        $element = $dom->importNode($ele, true);
        $dom->appendChild($element);
        return $dom->saveHTML();
    }

}

$html = '
    <body>
        <div class="container" id="Youtube" style="height:100%;width:100%;border:none;"></div>
        <div class="inner"></div>
     </body>';
$xml = simplexml_load_string($html);
$tags = $xml->xpath('//body/div[@id="Youtube"]');

$string = CeiXML::asHTML($tags[0]);

I hope that help you

HMagdy
  • 3,029
  • 33
  • 54