I have a string with html code. It has lot of urls formats. I used preg_match_all to get all the urls. Now i want to loop through $match2 array and only print the urls that has ".m3u8" some where in the full url. var_dump($match2) prints all the urls correctly but my for loop keep giving me this error;
E_NOTICE : type 8 -- Array to string conversion
could any one help me fix this problem ?Thanks in advance.
php code:
$regex2 = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i";
preg_match_all($regex2,$code,$match2);
var_dump($match2);
for($i = 0; $i < count($match2); $i++){
if (strpos($match2[$i],'.m3u8') !== false) {
echo "<br>".$match2[$i];
} else {
//do nothing
}
}
I changed $match2 to $match2[0] and it wroked!
Related
I have a string localhost/uploads/images/photo.jpg, I just want to simply rename it to localhost/upload/images/photo_thumb.jpg, keeping in mind that is a dynamic data and am just using it to simply what I want to achieve. I have tried to use the strpos() function to get the position of the '.' then use it for something(this where I got lost), then another thought came to me use implode. I will appreciate if some can help me figure this out thanks.
<?php
function get_google_image_url($type, $id, $column = 'image', $multi = "",$thumb=''){
if($multi ==''){
//check if there is thumbnail add _thumb before extention
if($thumb != ''){
$l= $this->db->get_where($type,array($type.'_id'=>$id));
$n = $l->num_rows();
if($n >0){
$value = $l->row()->$column;
$position = strripos($value,'.');
return $l->row()->$column;
}
}else{
$l= $this->db->get_where($type,array($type.'_id'=>$id));
$n = $l->num_rows();
if($n >0){
/$value = $l->row()->$column
$position = strripos($value,'.');
//this where i got stucked
//return $l->row()->$column;
}
}
}
}
that's the function am trying to implement it in, but what I want to achieve is what I stated above.
i think the best way here is to use pathinfo
something like that should work
$path_parts = pathinfo('localhost/uploads/images/photo.jpg');
$strNewName = $path_parts['dirname'].'/photo_thumb.jpg';
echo $strNewName;
I would do it by using regex and preg_replace:
$string="localhost/uploads/images/photo_thumb.jpg";
$replacement="$1/$2_thumb.$3";
$pattern= '/([\w+\/]*)(?=\/)\/([\w]+(?<!_thumb))\.(\w+)/i';
echo preg_replace($pattern, $replacement, $string);
code you can run it here
$1 = matching until last /
$2 = name of the file
$3 = file extension
feel free to improve the regex pattern if needed.
$klasseinput = strtoupper(trim($_POST["klasseliste"]));
$data = file('student.txt');
$data = array_filter($data);
foreach($data AS $row){
$student[] = explode(';', $row);
}
$antall = count($student);
for ($i = 0; $i < $antall; $i++){
if($klasseinput == $student[$i][3]){
print('<tr><td>'.$student[$i][0]."</td><td>".$student[$i][1]."</td><td>".$student[$i][2]."</td><td>".$student[$i][3]."</td></tr>");
}
}
/////////STUDENT.txt//////////
ph;petter;hanssen;IT1
gb;Geir;Bjarvin;IT2
mj;Marius;Johansen;IT3
/////////////////////////////
I am trying to compare an input form with an item in the multidimension array, but even tho the variable from the input field is exactly the same as the value in the array, it doesnt pass the if check.
$student[0][3] = IT1
$student[1][3] = IT2
$student[2][3] = IT3
If you have made sure there is no white space spoiling the comparison, then you might find a function like this useful to look at the strings on both sides of the comparison. You might find there are spurious characters causing trouble.
function hexdump($str)
{
for($i=0; $i<strlen($str);$i++)
{
echo "[$i] [".bin2hex($str[$i])."] [".$str[$i]."]<br />";
}
}
For instance, the string read from the file might contain CR LF characters. You could get rid of them using str_replace().
Thanks to Ravinder Reddy for the answer that was simple and worked for me:
" trim the value for \t\n if($klasseinput == trim($student[$i][3])){ "
I want to add some html to a word based on the words in a compound word.
For instance let's take the word "doghouse'
Let's say
$row2['word'] = 'doghouse';
$word = 'dog';
$otherword = 'house';
I want the end result to produce
<span style="color:blue">dog</span><span style="color:red">house</span>
My code so far that does not work (which I thought I could array the replace in str_replace but apparently I can't.
$finalword = str_replace(array($word,$otherword),array("<span style='color:blue'>".$word."</span>","<span style='color:red'>".$word."</span>"),$row2['word']);
Is str_replace the wrong choice for this?
str_replace is the correct function, but you're not really using it correctly. The way you've set it up, $row2 would have to be an array, but it's not, it's a string. So you need to use an alternate approach. Your code should look like this:
str_replace($row2, $word.$otherword, "<span style='color:blue'>".$word."</span><span style='color:red'>".$otherword."</span>");
The first term ($row2) is where you're searching, what php calls the 'haystack'. The second term $word.$otherword is what you're searching for (the 'needle'). The third term is what you replace the 'needle' with when you find it.
I don't know how do you store your colors, but does it work for you?
$row2['word'] = 'doghouse';
$words = array('dog','house');
$colors = array('blue', 'red');
$finalword = "";
for ($i = 0; $i < count($words); $i++) {
$finalword .= "<span style='color:".$colors[$i]."'>".$words[$i]."</span>";
}
echo $finalword;
I have a string which can be written in a number of different ways, it will always follow the same pattern but the length of it can differ.
this/is/the/path/to/my/fileA.php
this/could/also/be/the/path/to/my/fileB.php
another/example/of/a/long/address/which/is/a/path/to/my/fileC.php
What I am trying to do is cut the string so that I am left with
path/to/my/file.php
I have some code which I got from this page and modified it to the following
$max = strlen($full_path);
$n = 0;
for($j=0;$j<$max;$j++){
if($full_path[$j]=='/'){
$n++;
if($n>=3){
break 1;
}
}
}
$path = substr($full_path,$j+1,$max);
Which basically cuts it at the 3rd instance of the '/' character, and gives me what is left. This was fine when I was working in one environment, but when I migrated it to a different server, the path would be longer, and so the cut would give me too long an address. I thought that rather than changing the hard coded integer value for each instance, it would work better if I had it cut the string at the 4th from last instance, as I always want to keep the last 4 'slashes' of information
Many thanks
EDIT - final code solution
$exploded_name = explode('/', $full_path);
$exploded_trimmed = array_slice($exploded_name, -4);
$imploded_name = implode('/', $exploded_trimmed);
just use explode with your string and if pattern is always the same then get last element of the array and your work is done
$pizza = "piece1/piece2/piece3/piece4/piece5/piece6";
$pieces = explode("/", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
Then reverse your array get first four elements of array and combine them using "implode"
to get desired string
This function below can work like a substr start from nth occurrence
function substr_after_nth($str, $needle, $key)
{
$array = explode($needle, $str);
$temp = array();
for ($i = $key; $i < count($array); $i++) {
$temp[] = $array[$i];
}
return implode($needle, $temp);
}
Example
$str = "hello-world-how-are-you-doing";
substr after 4th occurrence of "-" to get "you-doing"
call the function above as
echo substr_after_nth($str, "-", 4);
it will result as
you-doing
Is there a way in php to do a preg_match on a url like below
dynamic/dynamic/dev/new_mobile/lib
and it would only pull out dev/new_mobile, and the link also has the ability to be like this too
dynamic/dynamic/dynamic/tst/new_mobile/lib
In the above example it would only pull out tst/new_mobile. The key is it would grab the last two directories before lib. Any idea how this could be done?
Here's a regex that will get the part you want:
$url = 'dynamic/tst/new_mobile/lib/foo/bar';
if (preg_match('#^(?:.*?/)?([^/]+/[^/]+)/lib(?:/.+)?$#', $url, $matches)) {
$part = $matches[1];
var_dump($part); // "tst/new_mobile"
}
This will get the 2 directories before the lib directory allowing for any directories before and after. It will also match a couple of edge cases that you don't mention whether you need:
tst/new_mobile/lib/foo/bar
/tst/new_mobile/lib/foo/bar
just explode then reverse array, simple and easy to use.
$array_url = explode('/',$url);
$tmp_array_url = array_reverse($array_url);
then you can rebuild as you will, no need to wonder with how many dynamic parts come before.
echo $tmp_array_url[0]; // "lib";
echo $tmp_array_url[1]; // "new_mobile";
echo $tmp_array_url[2]; // "dev";
EDIT: since you got lib constant, just do something like this :
$the_two_before = "";
for($i = 0; $i < count($tmp_array_url); $i++){
if($tmp_array_url[$i] == "lib"){
$the_two_before = $tmp_array_url[$i+2]."/".$tmp_array_url[$i+1];
}
}