str-replace

PHP replace words to links except images

∥☆過路亽.° 提交于 2019-12-02 11:35:52
问题 My code is $words = array(); $links = array(); $result = mysql_query("SELECT `keyword`, `link` FROM `articles` where `link`!='".$act."' ") or die(mysql_error()); $i = 0; while($row = mysql_fetch_array( $result )) { if (!empty($row['keyword'])) { $words[$i] = '/(?<!(src="|alt="))'.$row['keyword'].'/i'; $links[$i] = '<a href="'.$row['link'].'.html" class="ared">'.$row['keyword'].'</a>'; $i++; } } $text = preg_replace($words, $links, $text); I want to replace Hello with Guys except img src and

str_replace within certain html tags only

你。 提交于 2019-12-02 07:23:22
I have an html page loaded into a PHP variable and am using str_replace to change certain words with other words. The only problem is that if one of these words appears in an important peice of code then the whole thing falls to bits. Is there any way to only apply the str_replace function to certain html tags? Particularly: p,h1,h2,h3,h4,h5 EDIT: The bit of code that matters: $yay = str_ireplace($find, $replace , $html); cheers and thanks in advance for any answers. EDIT - FURTHER CLARIFICATION: $find and $replace are arrays containing words to be found and replaced (respectively). $html is

Why str_replace is not replacing correctly

给你一囗甜甜゛ 提交于 2019-12-02 06:57:48
问题 Here is my script $searchArray = array("Coupon Codes", "Coupon Code", "Promo", "Promo Codes"); $replaceArray = array("Promo Code", "Promo Codes", "Coupons", "Coupon Code"); $intoString = "Best Buy Coupon Codes"; print str_replace($searchArray, $replaceArray, $intoString); Result: Best Buy Coupons Code Expected Output: Best Buy Promo Code PHP Version 5.6.36 回答1: The reason for your unexpected result is that str_replace will first replace "Coupon Codes" with "Promo Code" and then it will later

Does PHP str_replace have a greater than 13 character limit?

倖福魔咒の 提交于 2019-12-02 05:47:08
问题 This works up until the 13th character is hit. Once the str_ireplace hits "a" in the cyper array, the str_ireplace stops working. Is there a limit to how big the array can be? Keep in mind if type "abgf" i get "nots", but if I type "abgrf" when I should get "notes" I get "notrs". Racked my brain cant figure it out. $_cypher = array("n","o","p","q","r","s","t","u","v","w","x","y","z","a","b","c","d","e","f","g","h","i","j","k","l","m"); $_needle = array("a","b","c","d","e","f","g","h","i","j",

My code with str_replace don't work

守給你的承諾、 提交于 2019-12-02 04:57:45
why this code isn't working? I was trying to rename, switch location and other, but it seems to be str_replace bug. It would be nice, if somebody told me, what's wrong... This is my index.php <?php header('Content-Type:text/html;charset=utf-8'); session_start(); require_once ('inc/core.php'); $core = new core($_GET['viev']); this is core.php <?php class core{ var $template; var $view; public function __construct($view) { $this->template = $this->loadTemplate(); $this->view = $view; $this->loadView(); echo $this->template; } private function loadTemplate(){ if(file_exists('template/template

PHP str_replace

梦想与她 提交于 2019-12-02 04:47:41
I have the string $var in which I need to replace some text. The first "X" needs to be replaced by "A", the second "X" needs to be replaced by B and so on, here is an example: <?php $var = "X X X X"; // input ... echo $var //the result: "A B C D" ?> I tried with str_replace but that doesn't work. Thanks You could use preg_replace 's limit argument to only replace once. <?php $var = 'X X X X'; $replace = array('A', 'B', 'C', 'D'); foreach($replace as $r) $var = preg_replace('/X/', $r, $var, 1); echo $var; ?> http://codepad.viper-7.com/ra9ulA You could use preg_replace_callback() : // as many as

Add _blank to all external links [duplicate]

徘徊边缘 提交于 2019-12-02 04:36:49
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Grabbing the href attribute of an A element Parse All Links That Contain A Specific Word In "href" Tag I'm using the following function to add _blank to all the links on my website. function targetBlank($text) { $return = str_replace('<a', '<a target="_blank"', $text); return $return; } I'm looking for a solution to apply this function only on external links (not on my domain) instead of all links. 回答1: Here's

PHP replace words to links except images

纵然是瞬间 提交于 2019-12-02 03:17:10
My code is $words = array(); $links = array(); $result = mysql_query("SELECT `keyword`, `link` FROM `articles` where `link`!='".$act."' ") or die(mysql_error()); $i = 0; while($row = mysql_fetch_array( $result )) { if (!empty($row['keyword'])) { $words[$i] = '/(?<!(src="|alt="))'.$row['keyword'].'/i'; $links[$i] = '<a href="'.$row['link'].'.html" class="ared">'.$row['keyword'].'</a>'; $i++; } } $text = preg_replace($words, $links, $text); I want to replace Hello with Guys except img src and alt. From Say Hello my dear <img src="say-hello-my-dear.jpg" alt="say hello my dear" /> I want Say Guys

Replace all kind of dashes

ⅰ亾dé卋堺 提交于 2019-12-01 23:41:29
问题 I have a excel document which I import in MySQL using a library. But some of the texts in the document contain dashes which I though I have replaced, but apparently not all of them. - , – , - <-all of these are different. Is there any way I could replace all kind of dahes with this one - The main problem is that I dont know all of the dashes that exist in computers. 回答1: Just use regex with unicode modifier u and a character class: $output = preg_replace('#\p{Pd}#u', '-', $input); From the

How can I replace white space in filename of uploaded file

南笙酒味 提交于 2019-12-01 17:44:26
问题 I'm making a SWF uploader and have my HTML form done. It works totally fine until I upload a SWF file with spaces in the name. How can I replace whitespace with underscores? I have tried... str_replace(" ","_", $file); ...and... preg_replace(" ","_", $file); 回答1: How can I replace whitespace with underscores? The \s character class will match whitespace characters. I've added the + quantifier to collapse multiple whitespace to one _ . If you don't want that, remove the + . $file = preg