Search remote page for a string [closed] - php

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Is it possible to search a remote page with a given URL for a given string, and indicate whether it exists, and if so, some indication of where it was found?
For instance, "Post Your Question" is included on this site https://stackoverflow.com/questions/ask.
In addition to searching just the page given by the URL, I would also like to search any JS or CSS links.

You can use the PHP Simple HTML DOM Parser to traverse through the contents of an html file.
Doing something like this:
$html = file_get_html('http://stackoverflow.com/questions/ask');
$htmlstring = $html->plaintext;
if(strstr($htmlstring, 'Post Your Question') === true)
// do your stuff here
Then to get the url to css or js:
foreach($html->find('link') as $css) {
$cssHref = $css->href;
//load the css, parse or whatever
}
foreach($html->find('script') as $script) {
$jsSrc = $script->src;
//load js, parse or whatever
}
From there you can grab the css or js source url and do what you want with it.

Related

How to Intercept a css get request [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm building a custom Themeroller and in order to reflect the styling changes in the page I was thinking that once the client makes a styling change ,I'll add a link:
http://my.domain/styles.css?param=someoval
Then, I'll intercept the get request, modify the css and serve it.
How can i do it with apache http server and php?
The styles.css file must be a php script, for example:
<?php
header("Content-Type: text/css");
$param = isset($_GET['param']) ? $_GET['param'] : null;
?>
body, html {
background-color: #FFF;
<?php if (isset($param)) : ?>
font-family: <?php echo $param; ?>
<?php endif; ?>
}
You can do it like below
Here your file css
<?php
header("Content-Type: text/css");
$param = $_GET['param']; // reciving params
if($param='somethin'){
echo ".div{ some css }";
}else{
echo ".p{ some css }";
}
your css could be called like below
<link rel="stylesheet" type="text/css" href="styles.php?param=someoval" />

Calling a function from a variable (PHP) [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a website I am making as a school project, and it appears I have jumped into something a bit too deep. I'm using PHP to create a page for each array object. In each page, I want to personalize the contents to match the page. For example, I have a page that gets created named Darryl and it's going to contain all my images. Other pages will be created and the contents of those pages will be created dynamically depending on the page.
What I am trying to do, is to change the img src using PHP and use a counter to make different images pop up. This is incredibly hard for me to explain.
I have this line of html code which contains the php(Which determines which images get shown:
<td><img style="width:11em;" class="magnify" src="<?php print $imgSrc; ?>"/><br><?php print $pageName; ?></td>
The part i am having issue with is the " print $imgSrc; " part. This is the area where my php will create the src path. It uses this variable $imgSrc to get the correct path.
That variable is:
$imgSrc = "images"."/".$pageName."/".counter();
Which writes the
It looks like you're confused about what a function is or perhaps when it gets executed. If this is all the code you have on your page:
$imgSrc = "images"."/".$pageName."/".counter();
<td>
<img style="width:11em;" class="magnify" src="<?php print $imgSrc; ?>"/><br>
<?php print $pageName; ?>
</td>
Then $imgSrc is only ever written to once. So whatever counter() does, it only does once. You could wrap the whole thing in a for loop and do away withe the counter() call. You could remove the local variable of $imageSrc and make the attribute defined on the fly, like so:
<td>
<img style="width:11em;" class="magnify" src="<?php print "images"."/".$pageName."/".counter(); ?>"/><br>
<?php print $pageName; ?>
</td>
Then if the counter() function is stateful itself (as in, it returns a different value each time you call it), you're all set.
You can make a stateful function like the following:
$counter_var = 0;
function counter() {
global $counter_var;
return $counter_var++;
}
But globals are frowned upon, because the create hard to trace side effects (and other reasons). Most people would tell you to use a for loop. I only mention it because it looks closer to what you were trying to do in your original code.

get HTML between two PHP functions [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
<html>
<body>
<?php startblock("content"); ?>
<p> Page content </p>
<?php endblock("content"); ?>
</body>
</html>
I want to get the content between the two php functions. If you look at the above example I want to get "<p> page content <p>" in a string.
Thanx in advance
You can use Output Buffering
<html>
<body>
<?php ob_start(); ?>
<p> Page content </p>
<?php $html = ob_get_clean(); ?>
</body>
</html>
Declare the functions like this:
function startblock()
{
ob_start();
}
function endblock()
{
$content = ob_get_flush();
return '+++' . $content . '+++';
}
Note that you cannot nest this. As far as I know, you cannot have multiple output buffers that have a name. Therefor, the parameter you added has no use.

Manipulating CodeIgniter-parsed text [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am using the template parser class in CodeIgniter to give the view (MVC) a designer-friendly look. I have a series of posts that I want to display, but on the front page I only want part of a post to show (first 200 characters) followed by a ... "Read More" link.
It is outputting the posts, but seems to be ignoring the PHP substr() function bc the text comes out full-length.
Inside the model class:
function __construct()
{
parent::__construct();
$this->load->library('parser');
$this->load->model('MPosts');
}
function index() // BASE_URL
{
$data = array("article_posts" => $this->MPosts->get_posts());
$this->parser->parse('VPosts', $data);
}
Inside the view:
<body>
{article_posts}
<h2>{title}</h2>
<p><?=substr("{post}", 0, 200);?>...</p>
<p>Read More</p>
<hr />
{/article_posts}
</body>
Since you are using a MySQL query to fetch the articles you can always use SUBSTRING(article_column_name,1,200) to only grab the first 200 chars, basically substr()
See: https://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring
Inside the parser->parse() code:
CI first passes your template through the load->view() processor, just like a regular view (so it executes PHP code inside your template),
then it passes it through the "{pseudovar}-replacer".
At this point I'm sure you understood the problem: substr() is applied to the string "{post}"
I can think of these options for your case:
do the substr() controller-side, probably the fastest solution to fix your issue
remove the Parser layer and use plain ol' PHP code, which is so fine
use a third-party über heavy full-featured template engine
Try echo instead of ?= .
People tend to leave code to load (model) libraries in constructors of the controller in which user-defined functions will invoke calls to user-defined functions implemented in model files.

PHP variables to javascript, does not work [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a problem with PHP and Javascript variable communication. I have this code:
<?php
$php_var = 'lol';
?>
<html>
<script type="text/javascript" charset="utf-8">
var php_var = "<?php echo $php_var; ?>";
alert(php_var);
</script>
</html>
This code does not work (as intended) for some reason. I cannot get the PHP variable's value passed down to Javascript variable. For some reason, Javascript completely ignores the php tags and assigns php_var a value of "". So it alerts the literal php code I put it as.
What am I doing wrong? I have been stuck on this problem for 3 hours. Is it my server's problem? (Using web hosting, dedicated). Thank you
Use json_encode() to ensure you get a valid JavaScript expression (otherwise characters such as newlines and quotes will break things):
var php_var = <?php echo json_encode($php_var); ?>;
You also need to ensure PHP is actually enabled for the file. This is usually achieved by giving the file a .php extension.
Use .php file extension and this will work.
If not then you variable won't have a value, you can see the exact issue by using something like firebug.
You say that the file where your code is located has an .html extension, it should be .php for it to render the php code.
Well, you can rename the file to .php and it should work or you can do the following thing:
Create a .htaccess file,
Add the following code ->
RewriteEngine On
<FilesMatch "(file.html)">
SetHandler php5-script
</FilesMatch>
save the file,
then in the html file add the following php line at the beggining ->
than you can write php code inside the selected html file
Or you can add the following rule
RewriteEngine On
file.html file.php

Categories