How i can remove words in php? - php

That is my Code :
<?php
$url = 'http://www.ebay.de/itm/321773181887';
$aufrufe_content = file_get_contents($url);
$aufrufe1 = explode( '<span id="vi-time-wrapperSection">' , $aufrufe_content );
$aufrufe2 = explode("</span></span>" , $aufrufe1[1] );
echo $aufrufe2[0];
?>
How i can remove the word "Restzeit: " ?
I read on other sites I can solve my problem with "str_replace" but I dont know how I can use that in my code.

It is as simple as that :
<?php
// ... the first 4 lines
$aufrufe2 = explode("</span></span>" , $aufrufe1[1]);
$temp = str_replace('Restzeit: ','',$aufrufe2[0]);
echo $temp;
?>

Related

How to get a specific cell value from a table element existing in a PHP array. Is this possible?

Guys, I have this piece of code:
<?php
$url = 'https://www.kitco.com/gold-price-today-europe/';
$content = file_get_contents($url);
$first_step = explode( '<div class="table-price--body-table--overview-detail">' , $content );
$second_step = explode("</div>" , $first_step[1] );
echo $second_step[0];
//print_r ($second_step);
?>
The output of this code if I run it on my browser via loading the corresponding PHP file is the following:
Now, my question is how can I get the value from the table's cell marked in the yellow frame and place it in another PHP variable to continue working with my code?
Is this possible somehow?
Thanks in advance, George.
Guys, I finally made it with the following code. Of course, a piece of code was taken from the correct answer of this post
<?php
$url = 'https://www.kitco.com/gold-price-today-europe/';
$content = file_get_contents($url);
$first_step = explode( '<div class="table-price--body-table--overview-detail">' , $content );
$second_step = explode("</div>" , $first_step[1] );
//echo $second_step[0];
//print_r ($second_step);
$doc = new DOMDocument();
$doc->loadHTML($second_step[0]);
echo $doc->saveHTML();
$rows = $doc->getElementsByTagName("tr");
foreach ($rows as $row) {
$cells = $row->getElementsByTagName('td');
// Keep in mind that the elements index start at 0
// so we want 0, 1, 2 to get the first 3.
for ($i = 0; $i < 3; $i++) {
if (is_object($cells->item($i))) {
$value[] = $cells->item($i)->nodeValue;
}
}
}
//print_r($value);
$Gold_Price_Per_Gram = $value[4];
echo '<h2>Gold Price: <b>'.$Gold_Price_Per_Gram.'</b> </h2>';
I am also attaching here the new output on my browser:

How to comment in and out php code inside a file

Using PHP how to comment in all php code inside certain php file
for example if i've the followig file
$file = 'myfile.php';
has only PHP code
<?php
$c = 'anything';
echo $c;
?>
I want using PHP to comment in (add /* just after open tag <?php and */ just before close tag ?>) to be
<?php
/*
$c = 'anything';
echo $c;
*/
?>
And also how to do the reverse bycomment out (remove /* */) to return back to
<?php
$c = 'anything';
echo $c;
?>
I've been thinking to use array_splice then doing str_replace then using implode and file_put_contents but still unable to figure out how to do this.
Update
Okay meanwhile getting some help over here, i was thinking about it and it comes to my mind this idea .... USING ARRAY!
to add block comment /* just after open tag <?php i will convert the content of that file into array
$contents = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
and then i can array push new element at position 2 with /*
and to do the reverse i will use unset($contents[1]); to unset element at postion 2 which means, /* will be gone
later on i can file_put_contents($file, $contents); to re-write the file again.
You can use PREG_REPLACE :
<?php
function uncomment($file_path) {
$current = file_get_contents($file_path);
$current = preg_replace('/\\/\\*(.+?)\\*\\//s', '$1', $current);
file_put_contents($file_path, $current);
return $current;
}
echo "<plaintext>" . uncomment("code.php");
?>
BEFORE :
AFTER :
I don't know why you want comment or uncomment the php code but I don't think it's a good way to do. I advice you to use variable or constant, like this :
One other way to enable or disable you code, is to use constant variable after the second time :
TOGGLE/UNTOGGLE COMMENT :
You will be able to do :
uncomment("code.php", "MYENV_DEBUG"); // uncomment
uncomment("code.php", "MYENV_DEBUG"); // comment
uncomment("code.php", "MYENV_DEBUG"); // uncomment
uncomment("code.php", "MYENV_DEBUG"); // comment
FIRST TIME :
SECOND TIME :
THIRD TIME :
Code :
<?php
function uncomment_header($name, $value) {
return '<?php define("' . $name . '", ' . $value . '); ?>';
}
function uncomment($file_path, $name) {
$current = file_get_contents($file_path);
$regex = '/<\\?php define\\("' . $name . '", (0|1)\\); \\?>/';
if (preg_match($regex, $current, $match)) {
$value = ($match[1] == 1) ? 0 : 1;
$current = preg_replace($regex, uncomment_header($name, $value), $current);
} else {
$header = uncomment_header($name, 1) . "\n";
$start = 'if (' . $name . '):';
$end = 'endif;';
$current = $header . $current;
$current = preg_replace('/\\/\\*(.+?)\\*\\//s', $start . '$1' . $end, $current);
}
file_put_contents($file_path, $current);
return $current;
}
echo "<plaintext>" . uncomment("code.php", "MYENV_DEBUG");
?>
There are two types of comments in PHP 1)single line comment 2)Multiple line comment for single comment in php we just type // or # all text to the right will be ignored by PHP interpreter. for example
<?php
echo "code in PHP!"; // This will print out Hello World!
?>
Result: code in PHP!
For multiple line comments multiple line PHP comment begins with " /* " and ends with " / " for example
<?php
/* This Echo statement will print out my message to the
the place in which I reside on. In other words, the World. */
echo "Hello World!";
/* echo "My name is Noman Ali!";
echo "PHP Programmer!";
*/?>
Result: Hello World!
Like this:
#canned test data, a string and the file contents are the same
$contents = <<<'CODE'
<?php
$c = 'anything';
echo $c;
?>
CODE;
$contents = preg_replace(['/<\?php\s/','/\?\>/'], ['<?php/*', '*/?>'], $contents);
echo $contents;
Output
<?php/*
$c = 'anything';
echo $c;
*/?>
Sandbox
NOTE - this will only work if the ending tag is present. In PHP the ending tag is actually optional. This will also not work on things like short tags <? or <?= although it will catch the ending tags.
Because of these edge cases it's very hard to do with regex (or any string replacement).
Valid examples of PHP code
<?php
$c = 'anything';
echo $c;
?>
//-------- no ending tag ---------
<?php
$c = 'anything';
echo $c;
//------- short tags ---------
<? echo 'foo'; ?>
//------- short echo tags ---------
<?= $foo; ?>
etc...
Good luck if you want to try to catch them all....

Loading an Array Elements with a PHP variable

Hello There I'm having a problem creating an array
<?php
//I'm actually grabbing the list from MySQl
//$list = '"02","03"';
$friends_list_array = array($list);
echo $friends_list_array[0];
?>
This is the Code !
But It Doesn't Work
Expected Result : 02
Output what i got from above code : "02","03"
Someone help please ?
Use php explode() function:-
<?php
$list = '"02","03"';
$friends_list_array = explode(",",$list);
echo $friends_list_array[0];
?>
Output:-https://eval.in/839531
If you want output strictly 02:-
<?php
$list = '"02","03"';
$friends_list_array = explode(",",$list);
echo trim($friends_list_array[0], '"');
?>
Output:-https://eval.in/839537
You can try also this way:-
<?php
$friends_list_array = array(
"02",
"03"
);
echo $friends_list_array[0];

How to get Implode get data before this string?

My url is
likehttp://localhost/manishatutors/tutors-in-city/Crossing-Republik-tutor/
how could i get Crossing Republic
using php
I used
<?php
list($a,$page_get) = explode("city/",$_SERVER['REQUEST_URI']);
$array=explode("/",$page_get);
$getCity1=remove_dash($array[0]);
$p=$array[1];
$get_city = implode('-',$getCity1);
print_r($get_city);
?>
but its giving
Crossing republik tutor
while I don't want tutor
use explode function and take the last
$req_uris = explode('/',$_SERVER['REQUEST_URI']);
echo $req_uris[count($req_uris)-1];
and if you want you can replace dash with space
echo str_replace('-', ' ', $req_uris[count($req_uris)-1]);
EDIT
$url = 'http://localhost/manishatutors/tutors-in-city/Crossing-Republik-tutor/';
$exploded = array_values(array_filter(explode('/',$url)));
$last = $req_uris[count($exploded)-1];
echo str_replace( '-', ' ', str_replace('tutor', '', $last) );
change $url with $_SERVER['REQUEST_URI']
You may try this
<?php
list($a,$val) = explode("city/",$_SERVER['REQUEST_URI']);
$array=explode("/",$val);
$val2=remove_dash($array[0]);
$p=$array[1];
$val3= implode('-',$val2);
print_r($val3);
?>
NEW EDITED ANSWER
list($a,$val) = explode("city/",$_SERVER['REQUEST_URI']);
$array=explode("/",$val);
$val2=$array[0];
$p=$array[1];
$val3= implode('-tutor',$val2);
print_r(remove_dash($val3[0]));
?>

Rename the uploading Image

i am uploading the image into the server , need to place the _ in place of gap in the image. Like if the name of image is Stack Flow.jpg, i need to send it as Stack_Flow.jpg in the directory as well in the email. HOw could be possible with the following code. i have tried but no success.. I am sending the 4 files in one form, code as ---
$filea = $_FILES['FILE1']['name'];
$fileb = $_FILES['FILE2']['name'];
$filec = $_FILES['FILE3']['name'];
$filed = $_FILES['FILE4']['name'];
$order_image_a='order_'.$orderId.'_'.$filea;
if(!empty($filea)) move_uploaded_file($_FILES['FILE1']['tmp_name'], "../files/$order_image_a");
$order_image_b='order_'.$orderId.'_'.$fileb;
if(!empty($fileb)) move_uploaded_file($_FILES['FILE2']['tmp_name'], "../files/$order_image_b");
$order_image_c='order_'.$orderId.'_'.$filec;
if(!empty($filec)) move_uploaded_file($_FILES['FILE3']['tmp_name'], "../files/$order_image_c");
$order_image_d='order_'.$orderId.'_'.$filed;
if(!empty($filed)) move_uploaded_file($_FILES['FILE4']['tmp_name'], "../files/$order_image_d");
i am using below function, how could i apply it for all four files--
<script>
function convertSpecialChars($str) {
$str = str_replace( " ", "_", $str );
return $str;
}
</script>
here is a quick example in php:
<?php
$name = "Stack Flow.jpg";
echo preg_replace('/[\s\-]+/', '_', $name );
?>
returns Stack_Flow.jpg
http://codepad.org/MQoEZ2wv
This is not a script but PHP..
<?
function convertSpecialChars($str) {
$str = str_replace( " ", "_", $str );
return $str;
?>
//do the same for all other images..
$filea = str_replace(' ', '_', $filea;
$order_image_a='order_'.$orderId.'_'.$filea;
if(!empty($filea)) move_uploaded_file($_FILES['FILE1']['tmp_name'], "../files/$order_image_a");
Using:
<?php
function convertSpecialChars($str) {
$str = str_replace( " ", "_", $str );
return $str;
}
?>
And then your code:
$filea = $_FILES['FILE1']['name'];
$fileb = $_FILES['FILE2']['name'];
$filec = $_FILES['FILE3']['name'];
$filed = $_FILES['FILE4']['name'];
$order_image_a='order_'.$orderId.'_'.convertSpecialChars($filea);
if(!empty($filea))
move_uploaded_file($_FILES['FILE1']['tmp_name'], "../files/$order_image_a");
$order_image_b='order_'.$orderId.'_'.convertSpecialChars($fileb);
if(!empty($fileb))
move_uploaded_file($_FILES['FILE2']['tmp_name'], "../files/$order_image_b");
$order_image_c='order_'.$orderId.'_'.convertSpecialChars($filec);
if(!empty($filec))
move_uploaded_file($_FILES['FILE3']['tmp_name'], "../files/$order_image_c");
$order_image_d='order_'.$orderId.'_'.convertSpecialChars($filed);
if(!empty($filed))
move_uploaded_file($_FILES['FILE4']['tmp_name'], "../files/$order_image_d");
Or if possible, you could do it in a loop (less duplicate code):
for ($i = 1; $i <= 4; $i++)
{
$file = $_FILES['FILE' . $i]['name'];
$order_image = 'order_' . $orderId . '_' . convertSpecialChars($file);
if(!empty($file))
move_uploaded_file($_FILES['FILE' . $i]['tmp_name'], "../files/$order_image");
}
In your code change $order_image_a='order_'.$orderId.'_'.$filea; and other similar lines to
$order_image_a='order_'.$orderId.'_'.convertSpecialChars($filea);
But will better if you will know how work your code.

Categories