Is it possible to find and replace a direct video link with PHP? I keep trying to use str_replace to find the link and replace it with an already specified one.
But is just leaves the text blank without the link there or anything.
Then it becomes <source src=""> With nothing inside of the src
INDEX.HTML (Video thing)
<source src="##LINK##">
INDEX.PHP
<?php
$title = htmlspecialchars($_POST['title']);
$link = htmlspecialchars($_POST['link']);
$desc = htmlspecialchars($_POST['desc']);
$pgname = htmlspecialchars($_POST['pagename']);
copy('index.html', $pgname . '.html');
$myFile = $pgname . '.html';
$ch = curl_init($myFile);
$output = curl_exec($ch);
$data = file_get_contents($myFile);
$replaceables = array("##TITLE##", "##PGNAME##", "##DESC##", "##LINK##");
$new = array($title, $pgname, $desc, $link);
$data = str_replace($replaceables, $new, $data);
file_put_contents($myFile, $data);
?>
Related
I want to ovewrite some labeled data in PHP Config file which looks like this:
define("__dbhost__", '{DB_HOST}');
define("__dbname__", '{DB_NAME}');
define("__dbuser__", '{DB_USER}');
define("__dbpass__", '{DB_PASS}');
define("__dbport__", '{DB_PORT}');
So the file is really simple.
The way i'm trying to replace data is by:
$searchF = array('{DB_HOST}','{DB_NAME}','{DB_USER}','{DB_PASS}', '{DB_PORT}');
$replaceW = array('a','c','d','b','a');
$fname = "../app/config/database.php";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));
$content = str_replace($searchF, $replaceW, $content);
$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);
But once finished and after I open the file I get
Resource id #16
Inside of it.
What's going on and why typical str_replace won't work for this case?
Just read the entire file into a string:
$searchF = array('{DB_HOST}','{DB_NAME}','{DB_USER}','{DB_PASS}', '{DB_PORT}');
$replaceW = array('a','c','d','b','a');
$fname = "../app/config/database.php";
$content = file_get_contents($fname);
$content = str_replace($searchF, $replaceW, $content);
file_put_contents($fname, $content);
Read file into string
Replace on that string
Write string to file
How to replace url with php
why this code not work .
please help
$mylogo = 'http://example.com/images/logo.png';
$inputmylogo = 'http://example.com/image.png';
$file = 'setting.php';
$content = file_get_contents($file);
var_dump(preg_replace('/'.$mylogo.'/', $inputmylogo, $content));
You want to employ str_replace here:
$mylogo = 'http://example.com/images/logo.png';
$inputmylogo = 'http://example.com/image.png';
$file = 'setting.php';
$content = file_get_contents($file);
var_dump(str_replace('/'.$mylogo.'/', $inputmylogo, $content));
this code not display title when i get link from txt file
<?php
function page_title($url) {
$fp = file_get_contents($url);
if (!$fp)
return null;
$res = preg_match("/<title>(.*)<\/title>/siU", $fp, $title_matches);
if (!$res)
return null;
// Clean up title: remove EOL's and excessive whitespace.
$title = preg_replace('/\s+/', ' ', $title_matches[1]);
$title = trim($title);
return $title;
}
$file = fopen("link.txt","r");
$lien = fgets($file);
fclose($file);
print page_title($lien);
?>
this code not display title when i get link from txt file
blank screen
my link.txt countains :
http://google.com
When I run this code i get the word "Google" displayed on my screen. So i have to think that your code is doing what you want but that there is some other, perhaps host-specific, problem here.
OK I just broke it by putting a CR in the text file after the URL and I fixed it by changing
$lien = fgets($file);
to
$lien = trim(fgets($file));
I have a xml rss feed that I'm using on my website, with this code I'm generating html from xml file:
$html = "";
$url = "http://books.com/new_bookss/?format=xml";
$xml = simplexml_load_file($url);
for($i = 0; $i < 10; $i++){
$link = $xml->resource[$i]->book_link;
$title = $xml->resource[$i]->book_title;
$img = $xml->resource[$i]->image_url;
$html .= "<img src=\"$img\"><br>$title";
}
echo $html;
Generated $link and $img looks like this:
http://books.com/new_books/booktitle/ /*this is for $link*/
http://images.books.com/img/booktitle.jpg /* this is for $img*/
I have to change these urls that way:
http://books.com/new_books/booktitle/ to http://mywebsite/new_books/booktitle/
http://images.books.com/img/booktitle.jpg to http://mywebsite//img/booktitle.jpg
URLs structure looks same every time:
http://books.com/new_books/booktitle/
http://books.com/new_books/something/
http://books.com/new_books/else/
Stricture on my website is same:
http://mywebsite.com/new_books/booktitle/
http://mywebsite.com/new_books/something/
http://mywebsite.com/new_books/else/
Same for $img, so the only thing I have to change is books.com to mywebsite.com
This is how I did it:
$link = str_replace("books.com","mywebsite.com",$link);
Added after:
$link = $xml->resource[$i]->book_link;
I am trying to create a simplified code to insert images dynamically into a page based on user entry similar to BBCode.
For example, if one of my users types "I like ducks [image]ducks[/image]", I want to explode the [image]ducks[/image], search MySQL for the keyword "ducks", pull the image path & name from the database that matches, then display the image HTML code as well as the source to the image.
function image_replace($dimg){
list($title) = explode("[image]",$dimg);
$query_image = mysql_query("SELECT * FROM images WHERE image_title LIKE '%$title%'");
$fetch_image = mysql_fetch_array($query_image);
$image_path = $fetch_image['image_path'];
$image_filename = $fetch_image['image_filename'];
$image_source = $image_path.$image_filename;
$dimg = str_replace("[image]","<img src=\"$image_source\">", $dimg);
$dimg = str_replace("[/image]","</img>", $dimg);
$dimg = str_replace("$title", "", $dimg);
return $img;
}
image_replace($ducks);
The wall I'm hitting is how to replace the text inside a dynamically generated page if it exists - and leave the content alone if the code doesn't exist. Any ideas?
EDIT - Complicating the problem:
Thanks for helping! I used your input to make the following function:
function image_replace($string){
$matches = array();
preg_match('/\[image\](.*)\[\/image\]/', $string, $matches);
$image = $matches[1];
$query_image = mysql_query("SELECT * FROM images WHERE image_title LIKE '%$image%'");
$fetch_image = mysql_fetch_array($query_image);
$image_path = $fetch_image['image_path'];
$image_filename = $fetch_image['image_filename'];
$image_source = $image_path.$image_filename;
$image_url = "<img src=\"$image_source\"></img>";
$new_string = preg_replace('/\[image\](.*)\[\/image\]/', $image_url, $string);
return $new_string;
}
I need this to work regardless of how many instances it occurs (thus if my user writes [image]duck[/image] then two sentences later writes [image]cow[/image], I want the function to replace both with their respective result). As it stands now, with more than one instance, it errors (not a valid SQL resource) which makes sense since preg_match only looks for one. I tried creating a loop (while & foreach w/ preg_match_all) to try testing the concept - both created infinite loops and my web server admin isn't too happy :p
I would try doing it with preg_match to get the image_url and preg_replace to replace it:
$string = 'I like ducks [image]ducks[/image]';
echo 'Before: ' . $string . '<br />';
$matches = array();
preg_match('/\[image\](.*)\[\/image\]/', $string, $matches);
$image = $matches[1];
//Lookup image_url and throw it in an <img>
$image_url = 'http://blah.com'; //Don't forget <img>
$new_string = preg_replace('/\[image\](.*)\[\/image\]/', $image_url, $string);
echo 'After: ' . $new_string;
edit
$string = "<br />I like ducks [image]ducks[/image]<br />I like cows [image]cows[/image]<br />I like pigs [image]pigs[/image]";
echo 'Before: ' . $string . '<br />';
$matches = array();
preg_match_all('/\[image\]([^\[]*)\[\/image\]/', $string, $matches);
$image_names = $matches[1];
foreach($image_names as $image_name) {
//Perform your lookup on each name
//If it is valid perform the replace
//Gonna say cows isn't there to test
if($image_name != 'cows') {
$image_url = 'http://blah.com'; //result of lookup
$string = preg_replace('/\[image\]' . $image_name . '\[\/image\]/', $image_url, $string);
}
}
echo 'After: ' . $string;