PHP modify previously echoed text - php

Complete newbie to PHP, not quite sure how to handle this.
I have a hyperlink that I would like to modify the text of once it has been clicked. So:
$linktext = 'Click Me!"';
echo $linktext;
if (isset($_GET["foo"])) {
$linktext = "Click Me AGAIN!";
}
But this does not change the text of the original hyperlink. If I add another echo $linktext; to the end, it just prints an additional hyperlink. Is there any way to go back and modify the original text?

Personally, I'd hold the name of the line outside of it in a variable and include it.
This stops replication of the link section.
Something like this;
$link = 'Click Me!';
if (array_key_exists('foo', $_GET)) {
$link = 'Click Again!';
}
$linktext = '' . $link . '';
echo $linktext;

You will want to make the text dynamic by putting it in to a variable. By using the original code, I made the required modification:
$text = "Click Me";
if (isset($_GET["foo"]))
{
$text = "Click Me AGAIN!";
}
$linktext = ''.$text.'';
echo $linktext;
Do observe that there now is a $text variable that holds the text and will be modified if foo has been set.

Related

How can I get this word str_replace by PHP

I want to replace the <a href='http://example.org/'>this word</a> element. But the problem is that "this word" can be any word.
<?php
$link = "http://example.com";
$site = file_get_contents($link);
$ades = "<a href='http://example.org/'>this word</a>";
$bdes = "";
$site = str_replace($ades,$bdes,$site);
echo $site;
?>
'This word' is a variable
'This word' can be pink, blue, door etc.
How can I get it?
edited :
I just want to remove like these codes
blaasdsad
gertvb
ertvvuyrt
awceawce
8k9789k789k
and else
$ades = "<a href='http://example.org/'>this word</a>";
echo strip_tags($ades);
Just use strip_tags function to remove the html tags. The output will be a string with the color name.
More info about strip_tags Here!!!
If your question is how to access every tag of your actual page which has this form <a href='http://example.org/'>any text or word</a>, I would use preg_replace, which use a pattern to detect what to change (instead of a string).
For your string, it would render something like that:
<?php
$link = "http://example.com";
$site = file_get_contents($link);
// use a pattern
$ades = "/^<a href='http:\/\/example\.org\/'>.*<\/a>$/";
$bdes = "";
// use other function
$site = preg_replace($ades,$bdes,$site);
echo $site;
?>
Try this:
$var="blue";
$ades = "<a href='http://example.org/'>".$var."</a>";
$shortAdes=substr($ades,strpos($ades,$var),strlen($var));
echo $shortAdes;
it weill print the $var

Using str_replace within an already-existing function?

So right now, I have a simple function that I use to call some text content:
function htmlstuff() { ?>
<p>html text content here</p>
<? }
And on a page I call the text using:
<?php htmlstuff() ?>
Now, I need to figure out how to use "search and replace" for whatever text is in the function. I've tried things like
function str_replace($search,$replace,htmlstuff())
but I obviously don't know what the heck I'm doing. Is there any simple way to just search the text within the function and search/replace?
what do you really want to do?
if you want to search and replace and in the return variable of your htmlstuff function then
you are not too far away from the correct answer.
function htmlstuff() {
$htmlstuff = "<p>html text content here</p>";
return $htmlstuff;
}
echo htmlstuff();
str_replace($search,$replace,htmlstuff());
this should do the trick
if you just want to make the function htmlstuff more dynamic, then you should take a different approach. something like this:
function htmlstuff($html) {
$htmlstuff = "<p>".$html."</p>";
return $htmlstuff;
}
echo htmlstuff("html text content here");
It would seem like this:
function htmlstuff ($content = "Default text goes here")
{
echo "<p>" . $content . "</p>";
}
and then on another call you just htmlstuff("New text to go there");
And if I'm wrong correct me to solve the problem
<?php
$html_stuff = htmlstuff();
$search_for = "Hello, world!";
$replace_with = "Goodbye, world!";
$html_stuff = str_replace($search_for, $replace_with, $html_stuff);
echo $html_stuff;
function htmlstuff() {
echo '<p>html text content here</p> ';
}

Jquery send data within function param

I have this issue with my jquery code:
<?php
//I oppen a text document, read the text inside of it and write it inside my html page.
//When someone clicks on a line, I want to take that very same line and send it via select(String) function.
$handle = fopen($_POST['lien'], 'r');
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle);
echo "<div onclick='select(\" ".$buffer." \");'>".$buffer."</div><br/>";
//It works when I put a simple string within the select param:
//echo "<div onclick='select(\" text \");'>".$buffer."</div><br/>";
}
fclose($handle);
}
?>
The jquery code :
function select(text){
alert(text);
//$("#selected").html();
}
where do you guys think is the problem ?
Thanks :)
Maybe Your $buffer contains some double-quotes, which then interfere with surrounding double-quotes. Look at produced HTML code if this is the case, maybe You'll see something like this:
<div onclick='select("He said: "Freeze!"")'>
... which Javascript can't parse correctly. If this is the case, consider using:
echo "<div onclick='select(".json_encode($buffer).")'>";
Try to change line:
echo "<div onclick='select(\" ".$buffer." \");'>".$buffer."</div><br/>";
to
echo "<div onclick='select(\" ".rtrim($buffer)." \");'>".$buffer."</div><br/>";
or to:
echo "<div onclick='select($(this).text());'>".$buffer."</div><br/>";

how to use urlencode( ) in my example?

I checked php.net and read a few examples of how urlencode( ) works but somehow I just can't get it right. Can someone give me a hand?
it'll be a lot to example so hopefully my brief example would make sense.
I have a page called 2.php and it was called to show some contents of a .txt file choosen in 1.php.
I am told to make a link for 3.php and the link should look something like /3?filename=a.txt
with filename as GET parameter name and Ensure GET parameter value is urlencoded using the urlencode( ) function.
but I'm confused how and where I should put urlencode() to make it work.
I'll paste my 2.php code here...I simplified the codes a bit...
<?php
$fileContents = file("./aaa/" . $_GET["course"] . ".txt");
echo "<table border=\"1\">";
foreach($fileContents as $row)
{
echo "<tr>";
$contents = preg_split("/,/", $row);
foreach($contents as $eachline)
{
echo "<td>";
if(!(preg_match("/#/", $eachline)))
{
echo trim(ucfirst($eachline));
}
else
{
echo trim(strtolower($eachline));
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
echo "<a href='./1.php'>Choose another txt file</a><br/>";
echo "or<br/>";
echo "<a href='.3.php?'>Work with this txt file</a>";
?>
BUT…the 3.php option must have a query string appended to it: the name of the text file that was selected in 1, so instead of ./3.php, the url should be something such as ./3?filename=asdf.txt
Use “filename” as the GET parameter name. Ensure the GET parameter value is urlencoded using the urlencode( ) function.
but I'm just not sure how to get it to work....
You can wrap the part that should be url encoded in the function within the string:
$url = 'http://www.google.com?q=' . urlencode($search);
OR in html
http://www.google.com?q=<?php echo urlencode($search); ?>
Where . is the concatenation of 2 outputs.

Is there something wrong with this XML/PHP Code for Tumblr to Website?

I am trying to link a tumblr feed to a website. I found this code (As you can see, something must be broken with it as it doesnt even format correctly in this post):
<?php
$request_url = “http://thewalkingtree.tumblr.com/api/read?type=post&start=0&num=1”;
$xml = simplexml_load_file($request_url);
$title = $xml->posts->post->{‘regular-title’};
$post = $xml->posts->post->{‘regular-body’};
$link = $xml->posts->post[‘url’];
$small_post = substr($post,0,320);
echo ‘<h1>’.$title.’</h1>’;
echo ‘<p>’.$small_post.’</p>’;
echo “…”;
echo “</br><a target=frame2 href=’”.$link.”’>Read More</a>”;
?>
And i inserted the tumblr link that I will be using. When I try to preview my HTML, i get a bunch of messed up code that reads as follows:
posts->post->{'regular-title'}; $post = $xml->posts->post->{'regular-body'}; $link = $xml->posts->post['url']; $small_post = substr($post,0,320); echo '
'.$title.'
'; echo '
'.$small_post.'
'; echo "…"; echo "Read More"; ?>
Any help would be appreciated. Thank you!
That is PHP, not HTML. You need to process it with a PHP parser before delivering it to a web browser.
… it should also be rewritten so it can cache the remote data, and escape special characters before injecting the data into an HTML document.

Categories