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

13
  • 3
    youtube !== Youtube, use a case insensitive method (stripos in your example) Commented May 14, 2013 at 13:57
  • 2
    You should probably surround your entire string in single quotes... Commented May 14, 2013 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 Commented May 14, 2013 at 13:58
  • I guess it's time for the "You can not parse HTML with regex" question again ! Commented May 14, 2013 at 13:59
  • 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 Commented May 14, 2013 at 14:14

6 Answers 6

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)
Sign up to request clarification or add additional context in comments.

Comments

3

try:

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

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

Comments

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";
    }

    ?>

2 Comments

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?
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" ...
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>';

Comments

1

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

I always use strtolower() to avoid similar problems.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.