PHP - How: Find A Line Replace It And Return Just That Change Line?
what im trying to do is take a youtube embed code find the URL code for that video and remove all other parts of the code keeping only the URL of the video
after i get the URL by it self then replace the http://www.youtube.com/ part of the URL with http://i2.ytimg.com/vi/ to do this i know that i need something like this to get the URL of the video http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+) but how to only return just the URL is something idk how to do then use str_replace http://www.youtube.com/(?:v|cp)/" with http://i2.ytimg.com/vi/ so in the end im asking if anyone know how to remove all other codes i dont want and only keep the URL this may have to be done using "regex" im not sure as i dont know to much about regex and what it can do but does sound like it would help lol Similar TutorialsHi! Basically, I'm making a PHP file that lists things from a file, so if the file contained; '@-Line 1-@@-Line 2-@' it would produce <li><a href="#">Line 1</a></li><li><a href="#">Line 2</a></li>, but I want it so it changes the coding for 1 of every 2 lines. So I've came up with this code: $myFile = "list.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, 100); fclose($fh); $oldtxt = array("@-", "-@"); $newtxt = array('<li><a href="#">','</a></li>'); $newData = str_replace($oldtxt, $newtxt, $theData); $array = explode("</li>", trim($newData)); $lineno = count($array); $linenofinal = $lineno - 1; for($i=1;$i<$linenofinal;$i++){ if(($i % 2)=='0'){ } } The bit that I'm stuck on is inside the if statement. How could I get it to replace for example <li> in line $i with <li class="1in2"> Thanks in advance, Daviga404 =D hi, im using the following code to read the content of a file, and then add a line to the end of the file. Code: [Select] $file = dirname(dirname(dirname(__FILE__))).'/includes/settings.php'; $read = fopen($file, 'r')or die('Opening Error'); $content = fread($read, filesize($file)); fclose($read)or die('Close Error'); if(isset($_POST['cat_bg'])) { $write = fopen($file, 'w')or die('Opening Error'); $cat_bg = $_POST['cat_bg']; $new_content = $content.'$color[\'cat_header\'][\'background\'] = '.$cat_bg.';'; fwrite($write, $new_content)or die('Write Error'); fclose($write); } however i would like to delete a line from the file before the content in inserted as the new line i am inserting is a duplicate variable with a different value. Can anyone help? Hi there,
I am trying to wrap some content in a div, but when I render it, it is outputting the actual HTML. This is the line I have: add_filter( 'woocommerce_product_description_heading', 'rename_product_description_heading', 10, 1 ); function rename_product_description_heading( $heading ) { return __( '<div class="myclass">Additional Information</div>', 'woocommerce' ); }
which is outputting: <div class="myclass">Additional Information</div> rather than wrapping it in the div.
Can anyone help me with wrapping the "Additional Information" text in a div? Thank you :) The title says it all,..well as all u can see, there are many methods to accomplish this done but not before undergoing through a heavy tasking process. For instance, 1. Open the file 2. Reads the content of the file into array. 3. Find out the desire line of text that we want to replace. 4. Make that line of text as variable and assigns with the new data(that we want to replace) as its values. 5.open another file. 6.Write the whole contents to the file opened in 5. 7. Put the contents back to the file of original. Can anyone contributes a better way to get this done? Like replace/delete a specific line without affecting other characters or anything therein? Thanx. P.S reading and using str_replace to make the alteration always ends up in giving some kind of result beyond desired after the first round of code execution. Hey Guys, First Post! Yay I have a problemo, I feel really stupid about having to ask this question. But basically I can't get a new line when I echo output in a script I am writing for my website. Here is the code: echo $newsentry; echo '_______ Read More..'; I tried '\n' I tried using " (double quotes) I have used ' (single quotes) I have positioned the Line Feed '\n' at the begining of an output, and at the end. Neither made any difference. I tried using carriage return '\r' too. Nothing. I haven't coded in PHP for a while, well nothing basic (I know that sounds rediculas but it has been mostly Apps etc, Client Scripts blah blah blah, but I haven't had to echo with a new line for a few years. Probably the last website which I wrote with my friend. So I checked the PHP manual, and I am clearly missing something when I am reading it, I understood it to mean (which as you can see from my code, is what I am currently doing), that you could just somehow type: echo "some string and it carries on to the second line"; However as I first thought, this makes no difference. Could this be a configuration on the Box that hosts this Site?? Sorry for sounding so stupid guys, I appreciate your help. -Craig Hi all Would like to ask for help . Generally I would like to run a webpage which will display actual price + price movements of selected cryptocurrencies.
I´m recieving data from extrernal API. (JSON) This is how code looks like: <?php $json = file_get_contents('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd&include_market_cap=true;include_24hr_vol=true;include_24hr_change=true'); $bitcoinPrice = json_decode($json); $json = file_get_contents('https://api.coingecko.com/api/v3/simple/price?ids=dash&vs_currencies=usd&include_market_cap=true;include_24hr_vol=true;include_24hr_change=true'); $dashPrice = json_decode($json); $json = file_get_contents('https://api.coingecko.com/api/v3/simple/price?ids=netchain&vs_currencies=usd&include_market_cap=true;include_24hr_vol=true;include_24hr_change=true'); $netchainPrice = json_decode($json); $json = file_get_contents('https://api.coingecko.com/api/v3/simple/price?ids=pivx&vs_currencies=usd&include_market_cap=true;include_24hr_vol=true;include_24hr_change=true'); $pivxPrice = json_decode($json); function formatter($amount) { // $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY); return '$'.number_format($amount, 2);; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>Masternodes monitor</title> <!-- Font Awesome --> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css"> <!-- Bootstrap core CSS --> <link href="css/bootstrap.min.css" rel="stylesheet"> <!-- Material Design Bootstrap --> <link href="css/mdb.min.css" rel="stylesheet"> <!-- Your custom styles (optional) --> <link href="css/style.css" rel="stylesheet"> <!-- MDBootstrap Datatables --> <link href="css/addons/datatables.min.css" rel="stylesheet"> </head> <body> <table id="dtBasicExample" class="table table-striped table-bordered table-sm" cellspacing="0" width="100%"> <thead> <tr> <th class="th-sm">Coin name </th> <th class="th-sm">ROI </th> <th class="th-sm">Daily income </th> <th class="th-sm">Price </th> <th class="th-sm">24 change </th> <th class="th-sm">Volume </th> <th class="th-sm">Marketcap </th> <th class="th-sm">Node price </th> <th class="th-sm">Coins for MN </th> <th class="th-sm">Nodes </th> </tr> </thead> <tbody> <tr> <td><img src="/img/btc.png" width="15" height="15" title="Bitcoin logo" alt="Bitcoin logo" /> Bitcoin (BTC) </td> <td>Unknown</td> <td>Unknown</td> <td><?=formatter($bitcoinPrice->bitcoin->usd) ?></td> <td><?=formatter($bitcoinPrice->bitcoin->usd_24h_change) ?></td> <td><?=formatter($bitcoinPrice->bitcoin->usd_24h_vol) ?></td> <td><?=formatter($bitcoinPrice->bitcoin->usd_market_cap) ?></td> <td>Unknown</td> <td>Unknown</td> <td>Unknown</td> </tr> <tr> <td><img src="/img/pivx.png" width="15" height="15" title="Pivx logo" alt="Pivx logo" /> Pivx (PIVX)</td> <td>Unknown</td> <td>Unknown</td> <td><?=formatter($pivxPrice->pivx->usd) ?></td> <td><?=formatter($pivxPrice->pivx->usd_24h_change) ?></td> <td><?=formatter($pivxPrice->pivx->usd_24h_vol) ?></td> <td><?=formatter($pivxPrice->pivx->usd_market_cap) ?></td> <td>Unknown</td> <td>10,000</td> <td>1,536</td> </tr> <tr> <td><img src="/img/dash.png" width="15" height="15" title="Dash logo" alt="Dash logo" /> Dash (DASH)</td> <td>7.5%</td> <td>14,40 $</td> <td><?=formatter($dashPrice->dash->usd) ?></td> <td><?=formatter($dashPrice->dash->usd_24h_change) ?></td> <td><?=formatter($dashPrice->dash->usd_24h_vol) ?></td> <td><?=formatter($dashPrice->dash->usd_market_cap) ?></td> <td>$69,696.22</td> <td>1000</td> <td>4001</td> </tr> <tr> <td><img src="/img/ntx.png" width="15" height="15" title="Netchain logo" alt="Netchain logo" /> Netchain (NTX) </td> <td>350%</td> <td>0,12 $</td> <td>$0.00004</td> <td><?=formatter($netchainPrice->netchain->usd_24h_change) ?></td> <td><?=formatter($netchainPrice->netchain->usd_24h_vol) ?></td> <td><?=formatter($netchainPrice->netchain->usd_market_cap) ?></td> <td>$7</td> <td>100.000</td> <td>55</td> </tr> </tbody> <tfoot> <tr> <th>Coin name </th> <th>ROI </th> <th>Daily income </th> <th>Price </th> <th>24 change </th> <th>Volume </th> <th>Marketcap </th> <th>Node price </th> <th>Coins for MN </th> <th>Nodes </th> </tr> </tfoot> </table> <!-- SCRIPTS --> <!-- JQuery --> <script type="text/javascript" src="js/jquery-3.4.1.min.js"></script> <!-- Bootstrap tooltips --> <script type="text/javascript" src="js/popper.min.js"></script> <!-- Bootstrap core JavaScript --> <script type="text/javascript" src="js/bootstrap.min.js"></script> <!-- MDB core JavaScript --> <script type="text/javascript" src="js/mdb.min.js"></script> <!-- MDBootstrap Datatables --> <script type="text/javascript" src="js/addons/datatables.min.js"></script> <script> $(document).ready(function () { $('#dtBasicExample').DataTable(); $('.dataTables_length').addClass('bs-select'); setTimeout(function() { location.reload(); }, 900000); }); </script> </body> </html>
I need section usd_24h_change in percent %. I tryed : <td><($bitcoinPrice->dash->usd_24h_change) ? % ></td> <td><($dashPrice->dash->usd_24h_change) ? % ></td> <td><($netchainPrice->dash->usd_24h_change) ? % ></td> <td><($pivxPrice->dash->usd_24h_change) ? % ></td> But it doesn´t work… Please help Edited October 26, 2019 by NetchainHi. I want a simple textbox, that when submited, will replace very every new line, with the <br> tag. What will happen, when it submits, it will take the contents of a textbox, add the <br> tag where a new line is suposed to be, and save the string to a MySQL Database. I think this is the easiest way of allowing a user to edit what appears on a website when logged in, but if there is a easier way, then please tell me. What I am trying to do, is a login page, and once logged in, you enter new text into the textbox, click submit, and on the website homepage, the main text will change to what was submitted. But if there is a new line, I think the only way in HTML to make one is to put a <br> tag, so I want the PHP to but that tag wherever there is a new line. Sorry if I am confusing, I am not that advanced at PHP, but I would be very happy if you could supply me with the correct code. Time is running out... If you do not understand me, please tell me -- PHPLeader (not) Dear All Good Day, i am new to PHP (a beautiful server side scripting language). i want to send a mail with line by line message i tried with different types like by placing the things in table and using <br /> but the thing is the tags are also visible in the message of the mail. Here is my code: $message1 = "Name :". $_REQUEST['name']."<br />"; $message1 .= "Surname :". $_REQUEST['surname']."<br />"; $message1 .= "Cellphone :". $_REQUEST['mobileno']."<br />"; $message1 .= "Telephone :". $_REQUEST['landno']."<br />"; $message1 .= "Fax :". $_REQUEST['fax']."<br />"; $message1 .= "Company :". $_REQUEST['company']."<br />"; $message1 .= "Email :". $_REQUEST['email']."<br />"; $message1 .= "Country :". $_REQUEST['country']."<br />"; $message1 .= "Enquity :". $_REQUEST['enquiry']."<br />"; $message1 .= "Date&Time :". $date."<br />"; For this code if try to print/echo it it is working fine it is displaying line by line, but using this variable ($message1) in the mail these <br /> are also visible. Can any one guide me to resolve(to remove these tags from the message part) this issue. Thanks in Advance. :confused: i have to read a single line from a csv, its a really big file and i only need one column.
i need the response to be a string ,i made a search and found the following code but i dont have any idea how to get a single line from a single string per run .
<?php $row = 1; //open the file if (($handle = fopen("file.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 0, ",")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "<br />\n"; } } fclose($handle); } ?> Edited by bores_escalovsk, 16 May 2014 - 06:38 PM. How to get this echo line to display as one line? No matter what I have done it displays as two lines. I even tried <nobr></nobr> Teachers Name: John Jones $userid = mysql_real_escape_string($_GET['user_id']); $sql = "select * from users where `id`='$userid' "; $rs = mysql_query($sql); while($row = mysql_fetch_array($rs)) { echo "<h3>Teachers Name: </h3>" . $row["first_name"] . " " . $row["last_name"] ; } Thanks for your help. I have a script that reads a .gz file into an array and prints the name of each record but will not work on larger files. Is there a way to read 1 line at a time? Here is the code I have so far. Code: [Select] <?php if ($handle = opendir('.')) { print "<ol>"; while (false !== ($file = readdir($handle))) { if($file != '..' && $file!="." && $file!="start_update.php" && $file!="sharons_dbinfo.inc.php" && $file!="root.php" && $file!="read_directory.php" && $file!="read_dir.php" && $file!="new_category.php" && $file!="index.php" && $file!="file_count.php" && $file!="dir_loop2.php" && $file!="dir_loop1.php" && $file!=".htaccess" && $file!="Answer.txt" && $file!="Crucial_Technology-Crucial_US_Product_Catalog_Data_Feed.txt"){ $filename = $file; $go = filesize($filename); if($go >= 1){ $filename2 = explode("-", $filename); $filename2 = $filename2[0]; echo str_replace("_"," ",$filename2) . ' | Filesize is: ' . filesize($filename) . ' bytes<br>'; $gz = gzopen($filename, 'r'); $lines = gzfile($filename,10000); foreach ($lines as $line) { $line2 = explode(",", $line); $line2 = str_replace("," , "-" , $line2); echo "<li>".str_replace("," , "-" , $line2[4])."</li><br>"; } } } } closedir($handle); } ?> </ol> Hi i wrote a find and replace code but my code replace last value of array and skip the first, second.... values in the array. Please give me an idea because i stack with this code. Regards $KeyWord = explode("\n", $RowGetWords['KEYWORDS']); $Replace = explode("\n", $RowGetWords['ReplaceTo']); for($i=0; $i<count($KeyWord); $i++){ $pattern = $KeyWord[$i]; $replace = "<a href=\"" .$URL. "\" target=\"_blank\" >" .$Replace[$i]. "</a>"; $html = str_replace($pattern, $replace, $Row['MessageBody']); } I'm so sorry to ask such a "newbee question," but believe me I have been on Google for the better part of the week trying to find an answer.
I'll start with the brief question.
Then I'll give an example to show what I mean.
Then, I'll give the BESTEST "pseudo-code" I could come up with (to PROVE that I've really given it my best).
Question:
How do I make PHP loop through a big file, make the changes line by line, and to save the resultant file.
Example: I have a 100mb text file ("animals.txt") with 500,000 lines. If any lines have the word "cat" in it, I want to add "Be careful with cats!" to the end of the line:
From this:
A fish and his tank.
A cat and his toy.
A bird and her cage.
A frog and his lilly.
A cat and her friend.
To this:
A fish and his tank.
A cat and his toy. Be careful with cats!
A bird and her cage.
A frog and his lilly.
A cat and her friend. Be careful with cats!
The best "pseudo-code" I can come up with is:
<?php
$data = file_get_contents("animals.txt");
$lines = explode(PHP_EOL,$data); I want my application will send a email after 10 minutes of sending another email. In my application A user completes registration with payment Application sends the user a payment confirmation emailNow I want to send another email 10 minutes After payment confirmation email with welcome tipsBelow is the function where for user setup .
public function finishUserSetup($Sub){ if($Sub == 0){ $subscription = SubscriptionPlans::where('identifier', '=', "Monthly")->first(); $expiry = date('Y-m-d', strtotime('+' . $subscription->months . ' months')); $sub_period = "monthly"; } else{ $subscription = SubscriptionPlans::where('identifier', '=', "Annually")->first(); $expiry = date('Y-m-d', strtotime('+' . $subscription->months . ' months')); $sub_period = "annually"; } $this->expiry_date = $expiry; $this->user_type = "SUB"; $this->subscription_period = $sub_period; $this->update(); $replaceArray = array( 'fullname' => $this->forename . " " . $this->surname, 'subscriptionName' => $subscription->name, ); EmailTemplate::findAndSendTemplate("paymentconfirm", $this->email, $this->forename . " " . $this->surname, $replaceArray); } In the above function the last line of code is the one which sends a payment confirmation email to the user which is EmailTemplate::findAndSendTemplate("paymentconfirm", $this->email, $this->forename . " " . $this->surname, $replaceArray); I want to execute the following line of code 10 minutes after the above one
EmailTemplate::findAndSendTemplate("WelcomeTips", $this->email, $this->forename . " " . $this->surname, $replaceArray);
How to do that. that is running the last line of code 10 minutes after Hello again. I use echo "Rating:".$json['Rating]."/10"; and get in return: 7.5/10 - it's ok. But I want to replace 7.5 with 75.png (image), 6.3 with 63.png, 9.4 with 94.png... hope u understand. Know someone how I can do this? Thanks. I have a long string of html, and what I want to do, if find all occurrences of: /static (anything after)... and replace with: http://www.mydomain.com/static (anything after). What is the best and fastest way to do this? Thanks. hello I've some code and I want remove text between Publisher: and >. This text is not fixed. anyone can help me to find text between two char and replace them with "" using php? thank you I have this code reads the xml runs with no errors but will not input the data from the array in the <catparent> The script Code: [Select] <?php // isbn => pages $page_numbers = array( '1234' => 'hello parent', // insert data into catparent node '5678' => 'hello parent', // insert data into catparent node ); $dom = new DOMDocument(); $dom->load('edtest1.xml'); $xpath = new DOMXPath($dom); $items = $xpath->query('item'); foreach($items as $item) { $catcode = $item->getElementsByTagName['catcode']; $parent_code = $page_numbers[ $catcode[0] ]; $item->appendChild( $dom->createElement('catparent', $parent_code) ); } $dom->save('edtest_stack.xml'); ?> My XML file looks like Code: [Select] <?xml version="1.0" encoding="UTF-8"?> <items> <item> <catcode>1234</catcode> <catdesc>Systems - System Bundles</catdesc> </item> <item> <catcode>5678</catcode> <catdesc>Hard Drives - SATA</catdesc> </item> </items> Output XML looks like this as you can see it has created the <catparent> but will not insert the data from the array. Code: [Select] <?xml version="1.0" encoding="UTF-8"?> <items> <item> <catcode>1234</catcode> <catdesc>Systems - System Bundles</catdesc> <catparent></catparent></item> <item> <catcode>5678</catcode> <catdesc>Hard Drives - SATA</catdesc> <catparent></catparent></item> </items> Any ideas pointers as to where I'm going wrong would be most helpful Hi ,, my frineds , how r u ? I have this article in my script: Quote <<WordPress>> widgets are the dynamic objects which eases the customization of the content on <<sidebars>> and widgetized <<footers>>. <<Widgets>> allows drag-n-drop interface in the Dashboard <<admin panel>>, for easy <<management>>. I want from the script , search about any word have <<..>> and replace it by: <a href="the word.php"> the word <a/> for examle : when the script find this word: <<Hello World>> , directly update it and replace PHP Code: <a href="hello-world.php"> Hello world </a> how can work it .. thank you very much for helping me .. ^_^ This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=328966.0 |