PHP - How Can Find Some Of Words And Replace It ..
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 .. ^_^ Similar TutorialsHi 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'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 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. 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 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, 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 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=328966.0 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 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 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! 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 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 The situation is that I have a large string with some identifiers to say "replace me" with something else. My goal is to replace the values, but if one replacement includes one of the other keys, I don't want to replace it.
For example:
<?php $str = 'some blob ~abc~ followed by ~def~'; $arr_orig = array( '~abc~', '~def~' ); $arr_new = array( 'value~def~', 'blah' ); echo str_replace( $arr_orig, $arr_new, $str ); ?>This will echo: some blob valueblah followed by blah But that's not what I'm trying to accomplish. Basically, I want ~abc~ to be replaced with the literal: value~def~ and for the ~def~ that happens to be part of that string to NOT be replaced. I hope what I'm asking is clear. Basically, preg_replace/str_replace with arrays seems no different than just doing a FOR loop and replacing ~abc~ and then ~def~. But that's not what I need. I'm hoping that if ~abc~ is replaced with text that happens to be another identifier -- ~def~, ~xyz~, whatever -- that this already replaced text is not again replaced. Is there a built-in function to do something like this? If not, should I just parse the whole string and count characters or something? Seems like a pain (and slow!) Hi, bit stuck on how to find and replace "<" and ">" with "<" and ">". I basically have a database record that outputs to screen and I need the code in the <code> tags to be rendered to the screen. I therefore need it to go through the whole array variable from the db and change the symbols just inside the code tags. Please be aware that the code tags might happen more than once here's an example below Code: [Select] <p>blah blah blah</p> <p>blah blah blah</p> <p>blah blah blah</p> <code> <h1>hello</h1> </code> <p>blah blah blah</p> <p>blah blah blah</p> <p>blah blah blah</p> <code> <h1>hello</h1> </code> the desired output would be: <p>blah blah blah</p> <p>blah blah blah</p> <p>blah blah blah</p> <code> <h1>hello</h1 </code> <p>blah blah blah</p> <p>blah blah blah</p> <p>blah blah blah</p> <code> <h1>hello</h1 </code> help on this would be great Cheers Rob : : NEW Problem I want to replace bad words which someone has inputted into my HTML textarea, with something like [CENSORED]. Here's what I've got so far: note: $review is the posted textarea name. Code: [Select] $words_text = 'badwords.txt'; $bad_words = file($words_text); $good_words = str_ireplace($bad_words,'[CENSORED]',$review); echo $good_words; The thing is, if I type bad words in the textarea, I still get bad words when I echo $good_words. I don't know why I'm getting this, any ideas Hello dear friends, I've an idea so let us say we have paragraph with bad words Code: [Select] $para = "Hello shit oh shit what the hell this is shit day"; Now let say we create textarea Code: [Select] <textarea id="badword" name="badword"> </textarea> and let us say we have database table as following Code: [Select] CREATE TABLE `banned` ( `id` varchar(50) NOT NULL default '', `badwords` text NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; consider we have connetion and can update mysql table badwords 1- now how we can enter word per line in this textarea example we entered in this textarea per line shit hell die death then by click on submit it stored in an database table 'banned' as INSERT INTO `banned` VALUES ('1', 'shit,hell,die,death'); 2- if we called `badwords` from table 'banned' how then can put 'shit,hell,die,death' into array so we can exclude it from $para can someone please help me in this , how to store textarea per line as sperated words in database table and how then call it back as array to exclude it form our paragraph Or it there any other simple way doing the same thank you Hi there, I'm making an online clothing store for the brick and mortar shop we have and would like the customer to be able to refine their search by size, colour etc. The size options will be XS, S, M etc etc where XS is 0, S is 1 and so on. So when they select XS it will add ?size=0 to the URL, if they select XS and S it will be ?size=0;1 What I need to know is how to make some PHP code which can recognise the ; is a break and know that both sizes are selected. Hope that makes sense and thanks in advance I would like to get all words from a site in a array. I found this great thing: http://simplehtmldom.sourceforge.net/ however it makes use of: file_get_contents which gives me: Warning: file_get_contents() [function.file-get-contents]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in And my hosting doesn't allow a change for that, they tell me to use curl. With curl i get this: Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in It's caused by this line: curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1); But setting it to 0 cause then instead of getting the word from the page i want i get the following words in a array: 302 Moved The Document has moved here What can i do now? I'm pretty sure the problem is starring me in the face, but I can't seem to locate it. function filter($string) { //swear words pulled from bannedwordlist.com $f = fopen('../badwords.txt', 'r'); $bad_words = fread($f, filesize('../badwords.txt')); $bad_words = explode('\n', $bad_words); $input = strtolower($string); foreach($bad_words as $value) { $string = str_replace($value, '****', $input); } return $string; } UPDATED CODE function filter($string) { //swear words pulled from bannedwordlist.com $f = fopen('../badwords.txt', 'r'); $bad_words = fread($f, filesize('../badwords.txt')); $bad_words = explode('\n', $bad_words); $input = strtolower($string); $string = str_replace($bad_words, '****', $input); return $string; } I've already echoed out $value in the foreeach loop, and it does correctly retrieve the bad words and put them into an array. My only problem is, the returned string is still in strtolower() form, and the words aren't censored. :/ |