Parse title from source code - php

i want to parse title from pages code source using title tag, someone made this for me but i dont know whats the problem because its not working, pls if anyone can help i really will appreciate that.
thank you.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title></title>
<?php
$url; = '';
preg_match("#<title>(.*)</title>#Ui", file_get_contents($url), $title);
$title = substr($title[1],0,255);
echo "Title $url : ' $title '<br>";
?>
</head>
</html>

You gotta fix this line: $url; = ''; to remove the semicolon after $url. Also, you should echo the result between the <title> and </title> tags, not after them. And lastly, a better structure for your application will be to execute PHP and then output HTML.
<?php
$url = ''; // <-- insert the URL of choice here, within quotes, starting with http://
preg_match("#<title>(.*)</title>#Ui", file_get_contents($url), $title);
$title = substr($title[1],0,255);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>
<?php echo $title; ?>
</title>
</head>
</html>

Related

$_GET in PHP not getting utm values

I am trying to print / echo utm_source, utm_medium and utm_campaign query values using this PHP code;
echo $_GET['utm_source'];
But strangely for some unknown reason on my server its not printing the values, when I replace utm_source to something else like test_source I am able to see the value in print.
Never faced any such issue, can anyone guide me here.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php echo $_GET['utm_source']; ?>
</body>
</html>
If your site is using WPengine, you will not be able to get the utm variables (and any additional variables after the utm's) out of the URL using PHP. Some web hosts specifically target and remove these utm parameters from server requests for performance reasons. Tricky part is, utm variables are still going remain in the URL which means that you can retrieve them using Javascript instead if you need to.
Source: WPengine article
https://wpengine.com/support/utm-gclid-variables-caching/
Your original question quoted this code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php $_GET['utm_source']; ?>
</body>
</html>
If you add an echo to this statement it will work
<?php echo $_GET['utm_source']; ?>
when tested using
test.com/test.php?utm_source=1
A safer piece of PHP code would be
<?php
if ( isset($_GET['utm_source']) ) {
echo $_GET['utm_source'];
} else {
echo 'the parameter utm_source is missing';
}
?>
I know it's late. but maybe this answer helps another developer.
// get the value of outlink, source, campaign, medium, content
let url = new URL(window.location.href);
let outlink = url.searchParams.get('outlink'),
utmSource = url.searchParams.get('utm_source'),
utmCampaign = url.searchParams.get('utm_campaign'),
utmMedium = url.searchParams.get('utm_medium'),
utmContent = url.searchParams.get('utm_content');
console.table( { outlink , utmSource, utmCampaign,
utmMedium,utmContent});

Print html content in php function

I'm trying to create a function 'header' which will print html content (doctype, html, head, body, etc) - but when I'm looking in a site source, all of that stuff is in one line, not in a tree hirearchy...
public function header() {
print(
'<!DOCTYPE HTML>'
. '<html>'
. '<head>'
. '<meta charset="utf-8"/>'
);
And when I'm looking in the web source the output looks like:
<!DOCTYPE HTML><html><head><meta charset="utf-8"/>
I would like it to look more like standard html tree:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
How can I do that? What are the options ?
EDIT:
Some of You showed me an echo option - it works, but it looks really bad in a php file - like:
public function header() {
echo "<!DOCTYPE HTML>
<html>
<head>
<meta charset='utf-8' />
</head>
<body>
</body>
</html>
";
The most classic way, using echo :
echo '<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
';
see below methods for printing HTML inside PHP code block
FOR SHORT HTML CONTENTS
echo ' <div class="myClass"> Some Text here. Some text here </p> ';
FOR SHORT HTML CONTENTS WITH PHP variable concatenation
$myName='Optimum';
echo ' <div class="myClass"> My Name is '. $myName .' </p> ';
FOR LONG CONTENT
$html.='';
$phpVariable ='Dummy content'
$html.='<div class="wrapper">'
$html.='<div class="content">';
$html.='<p> My content here'. $phpVariable .' </p>';
$html.='</div>';
$html.='</div>';
echo $html;
According to your scenario
<?php
public function header() { // SOME NECESSARY PHP CODE ?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="<?php //echo get_chartset ?>"/>
<link rel="stylesheet" type="text/css" href="<?php //echo_css_path ?>">
}
?>
This will echo/ print clean HTML code in front.
You could introduce tabs when you print:
print("<!DOCTYPE HTML>"
. "<html>"
. "\t<head>"
. "\t\t<meta charset=\"utf-8\"/>"
);
Another approach would be to simply just separate your html template file (Format it however you want) and then just require it with passed data in your function like so
my_view.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1><?= $heading ?></h1>
</body>
</html>
Your function
function showTemplate($view, array $data) {
extract($data);
require $view . '.php';
}
That way you can just call to output your view with data, like this
showTemplate('my_view', [ 'heading' => 'Awesome Page' ]);
This way your template and data would be more organized and pretty.
Another way, for doing this :
<?php
function myheader() {
?><!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<?php } ?>
I redefined the function name to avoid conflict, (off-topic).
I don't know how such a code should be indented ...

</script> in PHP heredoc

Why I am not able to end a javascript inside a PHP heredoc?
The rest of the code below this line:
</script>
become not part of PHP's code. They become HTML code.
It is like the end script code ends the PHP block.
$headerContent = <<<HEAD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>$title</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-2" />
<script>
</script> // Here is the problem
</head> // code here and below becomes not part of PHP.
<body>
.
.
.
HEAD;
Any tips for solving this problem?
Although I can't reproduce this with HEREDOC (it's possible that different versions of PHP behave differently in this respect), </script> is equivalent to ?> in PHP code, because it's a counterpart to <script language="php">. Example:
<script language="php"> $a = 1; </script>
Test: <?= $a ?>
So wherever you encounter problems with the ?> closing tag, you'll also encounter the same problems with the </script> closing tag. One option would be to store this in a variable and use it. Example:
<?php
$endScript = '</' . 'script>';
$headerContent = <<<HEAD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>$title</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-2" />
<script>
$endScript
</head>
<body>
.
.
.
HEAD;

PHP string with accentuated characters not displayed properly

I am on PHP 5.2.17 (and I am no PHP expert). I was hoping the following would display properly:
<?php
$title = "Jérôme";
echo $title."<br>";
?>
But it displays:
Jérôme
How can display my string properly? (The string is static)
Add to your HTML head:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
Note that you should have a proper HTML doctype because browsers default to non utf8. You can do a simple test, like I did, this works:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$title = "Jérôme";
echo $title."<br>";
But the place for the meta tag is in the head tag. The HTML document should look like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>An XHTML 1.0 Strict standard template</title>
<meta http-equiv="content-type"
content="text/html;charset=utf-8" />
</head>
<body>
<?php
$title = "Jérôme";
echo $title."<br>";
?>
</body>
</html>
That is standard.
<meta http-equiv="content-type" charset="utf-8" content="text/html;" />
<?php
$title = "Jérôme";
echo htmlspecialchars($title);
?>
Use php htmlentities function. see below example
$title = "Jérôme";
$title= htmlentities($title);
echo "<BR>Title :".$title."<br>";

Unwanted repeating html result from PHP

I don't know what is wrong with this code, it is a bug or I made a mistake somewhere; xDebug show nothing.
Class script
class theme {
function theme() {
//show header (meta, style, htmldoctype, script, and title)
$this->htmlheader();
//show main content
//show footer
}
function htmlheader() {
require "localsettings.php";
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n<html>\n<head>\n";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n";
echo "<title>$site_name - $page_title</title>\n";
echo "</head>\n";
}
}
index.php
require "theme.class.php";
$html = new theme();
//display result
$html->theme();
Output (incorrectly repeated)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>site title - </title>
</head>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>site title - </title>
</head>
When you name the function the same as the class, it is a "constructor", and gets called when the class is instantiated. Thus, your function theme() is called both here:
$html = new theme();
and here:
$html->theme();
Remove the latter, and you should be good to go.

Categories