PHP - Hi Is There A Way To Use Str_replace To Replace If Find Two Values?
hi is there a way of doing a str replace to look twice
like at moment i have if find space replace with "-" PHP Code: Code: [Select] .str_replace(' ','-',($row['model'])) is there a way i can do if find twice $row['model if find space " " replace with "-" also if find "i30" replace with "10" is there a way i can try find another like if find " " replace with "-" also if find "130" replace with hello world?? thanks in advance Similar TutorialsHello I'm making a guestbook in PHP and I'm having trouble displaying the posts. The user fills a form and sends it, it goes into the database (mysql) and I can then fetch the data. So far so good. I'm then going to use HTML to display the posts to the user. It's code looking like this: Code: [Select] <p> <br /> ID: ---replaceid--- <br /><br /> Time: ---replacetime--- <br /> From: <a href="---replacehomepage---">---replacefrom---</a> <br /> Email: ---replacemail--- <br /> <br /> Comment: ---replacecomment--- <br /><hr> </p> I want to use str_replace in PHP to replace the values in HTML... like this: Code: [Select] $numquery = mysql_query("SELECT * FROM guestbook", $dbconnect); $num = mysql_num_rows($numquery); $i=1; while ($i < $num) { header('Content-type: text/html'); $html = file_get_contents("test.html"); $query = mysql_query("SELECT * FROM guestbook WHERE id='$i'", $dbconnect); $rows = mysql_fetch_row($query); echo str_replace('---replaceid---', nl2br($rows[0]), $html); echo str_replace('---replacetime---', nl2br($rows[1]), $html); echo str_replace('---replacefrom---', nl2br($rows[2]), $html); echo str_replace('---replacemail---', nl2br($rows[3]), $html); echo str_replace('---replacehomepage---', nl2br($rows[4]), $html); echo str_replace('---replacecomment---', nl2br($rows[5]), $html); $i++; } But this only replaces ---replaceid---, leaves the rest and outputs it. Then, only ---replacetime--- is replaced and so on. I hope you understand. It means that 1 guestbook entry is displayed like 6, with only 1 string replaced at each one. What do you think I should do? Worth saying is that I'm not allowed to mix PHP-code with HTML-code 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']); } Hello, I need to be able to find what is inbetween 2 values in a string of text. For example, <?php $string="Hello and welcome to the site. Click [link]welcome.php[/link] here to continue to the site"; ?> So I need some method of searching the string to see what values are in between the [link] and [/link] which I will then deal with in my own way. I have a feeling that I could do this with explode, but I'm not really sure. Any advice? Thanks in advance 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. This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=328966.0 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 .. ^_^ I'm trying to increment a numerical value and after several hours of reading haven't come up with a solution. this may be too much information but here it is; I'm using this code to change the values in r5include.html Code: [Select] <?php $ident = $_POST['ident']; ?> <?php $ident2 = ($ident . 2); $filename = 'r5include.html'; $string = file_get_contents($filename); $old = array("'images/up.jpg'","add.html' target='_blank'"); $new = array("'$ident/$ident2.jpg'","$ident.html' target='iframe2'"); $data = str_replace($old,$new,$string); $handle = fopen($filename, "w+"); fwrite($handle,$data); fclose($handle); ?> but before I do that I'm copying the original to a temp file with this code: Code: [Select] <?php $filename = 'r5include.html'; $string = file_get_contents($filename); $tempfile = 'temp.html'; $handle = fopen($tempfile, "a"); fwrite($handle,$string); fclose($handle); ?> This is what goes to temp.html: Code: [Select] <img height=75 src='images/up.jpg' hidefocus='true' usemap='#record51' /> <map name='record51'> <area shape='rect' coords='0,0,75,75' href='add.html' target='_blank'> I would like to increment the 2 instances of record51 to record52 and increment again each time the process is initiated. And then append it to r5include with this: Code: [Select] <?php $myFile = "r5include.html"; $fh = fopen($myFile, 'a') or die("can't open file"); $filename = 'temp.html'; $string = file_get_contents($filename); fwrite($fh,$string); fclose($fh); ?> So the question is is it possible? and if so how do I increment the record number? Thanks, 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 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 know this won't be possible with DOC, but it should be with DOCX. Basically I want to have someone write up a docx file using special tokens in certain places. Then my PHP would find and replace those tokens when the users needs to. It would create and serve a NEW docx file with the tokens replaced. Any ideas on this? I'm reading up on a ton of libraries, but nearly all of them, their examples don't work out of the box. Thanks! Folks, I need to Find and Replace some words in Source HTML, which is saved in a PHP Variable. Example: Quote $html = '<img src="some_source" title="paintball mask Picture" alt="Image for Paintball Mask"> <strong> Best Paintball Mask</strong>'; Find Condition: I want to FInd word "Paintball Mask" in vairiable $html, only where if the word "Paintball Mask" is there in Htaml Tag <Strong> </Strong>. As you can observe in $html, we have "Paintball Mask" in <IMG> Tag also, i want to Skip this because I only Need to COnsider if the word "Paintball Mask" there in <strong></strong> or not. One the words is found in <strong></strong> tag, i need to replace that word, to make it a hyperlink. (I think this bit can be done with strreplace()) But how can i restict my search to only <strong></strong> tag? Let me give Data Again: Quote $html = '<img src="some_source" title="paintball mask Picture" alt="Image for Paintball Mask"> <strong> Best Paintball Mask</strong>'; Find What: Paintball Mask Find in Which Tag: <strong> </strong> Replace it with: <a href="/">Paintball Mask</a> can someone help me with the code? Cheers Natasha Thomas I wanted to replace a strings in a large text file .. what would be the fastest way ?! e.g the text file contains .. INSERT INTO `subjects` VALUES (1, 'some text here', 'some text here', 'some text here'); INSERT INTO `subjects` VALUES (2, 'some text here', 'some text here', 'some text here'); INSERT INTO `subjects` VALUES (3, 'some text here', 'some text here', 'some text here'); INSERT INTO `subjects` VALUES (4, 'some text here', 'some text here', 'some text here'); INSERT INTO `subjects` VALUES (5, 'some text here', 'some text here', 'some text here'); I wanted to replace the string " VALUES (1, " with "VALUES (" in each line .. I'm new in PHP so any response would be much appreciated .. thanks Hi, If i had the following array: $test = array('key1'=>'test1', 'key2'=>'value2', 'key3'=>array('test','test1','test2')); Is there a way i could change all the values that have the word "test" to "hello"? So basically end up with the following array: $test = array('key1'=>'hello1', 'key2'=>'value2', 'key3'=>array('hello','hello1','hello2')); Is this possible? Thanks Short question: How can I get a string of HTML code and replace all the <input> tags with their values? Long question: I have a web page with a purchase order on it in HTML format and one or more of the columns is editable via input boxes. So the user can change the quantity/length of the material before submitting it. Once they choose to save it, it sends the string of HTML to a PDF generator (DOMPDF) which converts the HTML to a PDF file and saves it to the server. The user then has a hard copy of the purchase order PDF saved for each job. The problem is DOMPDF doesn't do well with <input> tags, it just leaves them as a blank space. So I want to take that string of HTML and replace all input boxes with their values before sending it to DOMPDF. Currently I'm using Javascript to do just that when the user clicks to save the modified purchase order. The problem with this is it only works for when the user modifies the purchase order and saves a new modified copy. I need to save a base unmodified copy of it when the page loads so this would strip all the input boxes from the page as soon as it loads and the user would no longer be able to edit anything. So I need something to send the string to DOMPDF when the page loads but not modify what's on the page. Any ideas? Thanks. Hello, I am a newbie PHP hobbyist. I mess with php strictly for my own amusement. Up until now I have been able to Google my way to a answer to my questions but I'm lost on this one. I'm not really sure what to even search for. I have a few different mysql queries that returns the following arrays. qid15=37,37,37,37,37,37,37,37,37,37,37,38,38,39,39,41 qid17=47,47,47,47,47,48,49,49,49,50,50,50,51,51 qid18=52,52,52,52,52,52,52,52,54,54,55,55,55,56,56,56 qid19=57,57,57,57,58,59,59,61,61 What I would like to do is replace or create a new array for each result with the lowest value as 1 the next lowest as 2 then 3 and 4. My new array would look something like this oldqid15=37,37,37,37,37,37,38,38,39,39,41 newqid15=1,1,1,1,1,1,2,2,3,3,4 I sure would appreciate any ideas on this. I'm thinking there is probably a simple solution and before I waste anymore time I'll stop and ask for directions(as my wife would suggest). I know you can use str_replace to replace a value with another value, or an array of values with an array of other values. What I'd like to do is replace each instance of a single value with an array of values which get iterated through for each instance found. Think prepared statements that you don't have to call setString or setInt on. Here's a made up example. Code: [Select] $db->query = 'SELECT * FROM users WHERE user_name = ? AND user_role = ?; $db->params = array('hopelessX','admin'); $db->executeQuery(); //this contains a call to a private function that does the replace I have everything else working except this. It works if I specify an array of values to replace and do something like below, but I was hoping to not have to support that method of replace if I didn't need to. Code: [Select] $db->query = 'SELECT * FROM users WHERE user_name = :name AND user_role = :role; $db->replace = array(':name',':role'); $db->params = array('hopelessX','admin'); $db->executeQuery(); //this contains a call to a private function that does the replace I can do it that way if I need to or I could make some overly complicated thing that finds each instance of the question mark, replaces it with a unique value and then adds that unique value to a replace array similarly to the method in code block two but that defeats to the purpose of making this simple database adapter in the first place (although it was mostly just for fun anyway). Anyway, if anyone has any advice and knows of a simple means to achieve this goal that would be much appreciated. Thanks in advance Good Evening, I'm having no luck in something that I'm sure is incredibly simply... If I have an array like the following... Code: [Select] $array = array(15,22,0,43,0,6);How can I find and replace all the zeros with a one? Regards, Ace This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=315388.0 Im dealing with html links in a database where I need to execute a small piece of php code. Well mysql doesnt execute php code thats held in a table. The easiest way for me to do this is just add the links without the code, & set up a function that will replace anything I set to for example in my case a link lookin like this: http://mysite.com/index.php?page=offers&blah=blah&sid= needs to look like this; http://mysite.com/index.php?page=offers&blah=blah&sid=<?php echo $_SESSION['login']; ?> so I would need a function like <?php $replace('sid=','sid=<?php echo $_SESSION['login'];?> ?> something like that. And I know its possible & I could create a function like this but honestly right now I am so tired after working 12 hours im lazy dont want to think to hard while I know there are a lot of people out there that have a function like this already. If you dont have one & dont feel like writing one for me could you atleast point me in the right direction so I dont have to think to hard?! Thanks!! 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 |