Lets say i have a full source string of html page (in PHP), like:
<?php
$html_source = '
<html>
<head>
</head>
<body>
<div class="class1">Hello!</div>
<div class="class2">World</div>
</body>
</html>'; //PHP
?>
Then i want to use that php String as a jQuery $
Object for the purpose to use like:
alert(
$("div.class1").html(); //jQuery
);
So how do i convert the PHP string into jQuery Dollar Sign $
?
Note:
This is because my situation is... i have a Database contains full html pages sources. Then i have to parse them out but not all of whole page. Some DOM filtered areas. Like, "right panel" of that page source inside. Thats the problem.
Try using Zend framework/library's Zend_Dom_Query part. It understands most of CSS2, and you can always fall back to xpath when you need some more powerful.
If you would like to have a more JQuery like look-and-feel, you can try the phpquery project too.
Frist, make the string JavaScript safe. Given a string, json_encode
will make it JS safe (although not JSON).
You can then output it into a script block in your HTML and pass that to the jQuery function.
<script>
var foo = $(<?php echo json_encode($html_source); ?>);
</script>
If you need the html part or if you need to handle this with php than you can use php dom document extension.
try this
<script>
var myStr = $(<?php echo $html_source; ?>);
</script>