I have a very strange Problem. I am trying since 3 hours to the the output from $ht. When i echo $ht the result is http://www.google.com. There is no problem. But when i do file_get_contents with the variable $ht there is no output. I tried already with curl also no ouput - 404....
foreach ($hashtweet[1] as $ht)
{
$html = file_get_contents($ht); //NOT WORKING
$html = file_get_contents("http://www.google.com"); // WORKING
echo $html;
//echo $ht; = CORRECT OUT OF $ht = http://www.google.com
}
You should use trim function:
$html = file_get_contents(trim($ht));
Probably some spaces or other white characters are at the beginning or at the end of string
EDIT
In your question you told us the $ht is http://www.google.com but in comment you told is it's google.com. If you want to get http://google.com you need to add http://. Otherwise it means that you need in current directory file with name google.com
EDIT2
So you should compare those 2 strings. Add inside your loop:
if (trim($ht) == 'http://www.google.com') {
echo "same strings<br />";
}
else {
echo "there is difference<br />";
}
to make sure those strings are or aren't the same. You told that your $ht return string(21) "http://www.gooogle.com" so it's impossible it doesn't work because string http://www.gooogle.com string is exactly 21 characters long. Don't you change $ht variable before running file_get_contents or after it inside the loop?
Related
I got some files to change by clicking a button. To go for it, i have the old string to replace, saved in database, and also the new one.
On the click button, it executes a function that is gonna find the old string in the PHP file, then gonna replace it by the new one. (Final goal is to automate the PHP edits in a web software after an update).
My problem is that it perfectly works on short strings (without newline), but as soon as there is a newline into the file, nothing happens.
This is my actual code :
$path = '/mypath/' . $item['path'];
$old_code = $item['old_code'];
$new_code = $item['new_code'];
}
$pos = strpos(file_get_contents($path), $old_code);
$file = file_get_contents($path);
$str = str_replace($old_code, $new_code, $file);
file_put_contents($path, $str);
$pos is "true" if my $old_code doesn't have any newline.
I tried to use preg_match to remove \n, but the problem is that when i'll have to push my edits on the file with file_put_contents, every newline will also disapear.
Example of non-working str_replace :
echo "ok"; echo 'hey there is some spaces before'
echo 'this is a sentence';
$menu = ['test1', 'test200'];
print_r($menu);
$url = "/link/to/test";
$div = "echo \"<div class='central_container' align='center'>\";";
Do you have any idea for resolving this ?
Thanks
if I`m not wrong str_replace() work only with single lines . Its have 2 options.
Option line replace str_replace() with preg_replace() or just use https://regex101.com/ there also have code generator after you finish you Regex
I wrote a code which adds hyperlink to all plain text where it finds http:// or https://. The code works pretty well for https://www.google.com and http://yahoo.com. It converts these text into clickable hyperlink with correct address.
<?php
function convert_text_to_link($str)
{
$pattern = "/(?:(https?):\/\/([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])/i";
return preg_replace($pattern, "<a href='$0' target='_blank'>$0</a>", $str);
}
$str = "https://www.google.com is the biggest search engine. It's competitors are http://yahoo.com and www.bing.com.";
echo convert_text_to_link($str);
?>
But when my code sees www.bing.com, though it adds hyperlink to it but the href attribute also becomes www.bing.com. There is no http:// prepended it. Therefore the link becomes unusable without the link http://localhost/myproject/www.bing.com will go nowhere.
How can I add http:// to www.bing.com so that it should become http://www.bing.com?
Here is your function. Try this.
function convert_text_to_link($str) {
$pattern = '#(http)?(s)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])#';
return preg_replace($pattern, '$0', $str);
}
You should try and check if this works:
window.location = window.location.href.replace(/^www./, 'https:');
might be you will get your solution.
I just got to know about some other approaches too, you can try them out as per your code and requirements:
1.
str_replace("www.","http://","$str");
The test here is case-sensitive. This means that if the string is initially this will change it to http://Http://example.com which is probably not what you want.
try regex:
if (!$str.match(/^[a-zA-Z]+:\/\//))
{
$str = 'http://' + $str;
}.
hope this helps.
I have the following PHP code
$links = fopen("./links/links.txt", "r");
if ($links) {
while (($line = fgets($links)) !== false) {
$linkData = explode(" ", $line);
/// The line below is the problematic one
echo "<a href='".$linkData[0]."' class='links-item'>".$linkData[1]."</a><br>";
}
fclose($links);
} else {
die('Error opening file, try refreshing.');
}
You can see I've seperated the line I'm having issues with. I have the following file links.txt
http://example.com Example
http://example2.com Example2
Basically this will add the URL in the text file to an anchor tag, and it'll add the text next to it, as the anchor display text. It works, but for some reason, every anchor tag ends with a space, except the last one. Anyone know why this is and how I can fix it?
The string that fgets() returns includes the newline that separates the lines. This will be at the end of $linkData[1], so you're writing
<a href='http://example.com' class='links-item'>Example
</a><br>
to the output.
You could instead use fgetcsv(), specifying space as the field delimiter. This will explode the line for you and automatically ignores the newlines.
while (($linkData = fgetcsv($links, 0, " ")) !== false) {
echo "<a href='".$linkData[0]."' class='links-item'>".$linkData[1]."</a><br>";
}
fgets() captures newlines as well as word characters into the string. Use the trim function to remove unwanted whitespace:
echo "<a href='".trim($linkData[0])."' class='links-item'>".trim($linkData[1])."</a><br>";
Alternatively, as #Barmar noted, you could use fgetcsv function
use
var_dump($linkData);
To see what does fgets() returns. Maybe there are unexpected characters.
Consider to use more advanced file formatting, for example you can use csv format and use http://php.net/manual/en/function.fgetcsv.php to retrieve results from file.
I have a long string as a single line (its too long) and I want it to break and print as a new line whenever we meet a semi colon ; and } or { for example
This is my string:
aaaaaaaaaaa;bbbbbbbbbb{ccccccccc;dddddddd;}eeeeeee{fffffff;}
I want to print it as below:
aaaaaaaaaaa;
bbbbbbbbbb{
ccccccccc;
dddddddd;
}
eeeeeeee{
fffffffffff;
}
Even if ; and } meet together I wanna break it down and print in two lines.
For example jjjjjjjjjjjjj;} might display as
jjjjjjjjjjj;
}
Please help me.
You can simply use preg_replace as
$str = 'aaaaaaaaaaa;bbbbbbbbbb{ccccccccc;dddddddd;}eeeeeee{fffffff;}';
$res = preg_replace('/(;|{|})/',"$1\n",$str);
print_r($res);
Output :
aaaaaaaaaaa;
bbbbbbbbbb{
ccccccccc;
dddddddd;
}
eeeeeeee{
fffffffffff;
}
Regex
Demo
You can use preg_replace(/([;\{\}])/, '$1<br/>', $sourceLine) for HTML output. If outputing to file, change '$1<br/>' to "$1\r\n"
You can use like the below code also :
$str = 'aaaaaaaaaaa;bbbbbbbbbb{ccccccccc;dddddddd;}eeeeeee{fffffff;}';
$arr_replace_char = array(';','{','}');
$arr_replace_by = array(';<br>','{<br>','}<br>');
echo str_replace($arr_replace_char,$arr_replace_by,$str);
I am trying to get a piece of content out of a website that shows the current status from the server. I did this by the file_get_content method. When I try to search through it, i used a preg_match to find the item i want, but it ends up with html tags i cant seem to correctly escape within the code. I get an error Warning: preg_match() [function.preg-match]: Unknown modifier 'B'. So how do I escape this? and further I missed probably a few functions, but I am not really familair with the RegEx.
$content = file_get_contents('http://www.swgemu.com/forums/content.php');
$string = '<h3>Basilisk<\/h3><p>Status:<span class=\"online\">Online<\/span>';
if (preg_match($string , $content))
{ echo "The Basilisk server is online"; }
else
{ echo "The Basilisk server is offline";}
Thanks in advance.
It's a regex. Use a delimiter. Something like this -
$string = '/<h3>Basilisk<\/h3><p>Status:<span class=\"online\">Online<\/span>/';
Better use DOM parsers, but if you really need regex, try to allow spaces:
$content = file_get_contents('http://www.swgemu.com/forums/content.php');
$string = '/<h3>\s*Basilisk\s*<\/h3>\s*<p>\s*Status:\s*<span\s+class=\"online\">\s*Online\s*<\/span>/i';
if (preg_match($string , $content))
{ echo "The Basilisk server is online"; }
else
{ echo "The Basilisk server is offline";}