str-replace

How to skip first regex match?

故事扮演 提交于 2019-11-29 16:47:52
Is there anyway to skip the first match when using regex and php. Or is there some way of achieveing this using str_replace. Thanks UPDATE I am trying to remove all the instances of a string from another string but I want to retain the first occurance e.g $toRemove = 'test'; $string = 'This is a test string to test to removing the word test'; Ouput string would be: This is a test string to test to removing the word test Easy PHP way: <?php $pattern = "/an/i"; $text = "banANA"; preg_match($pattern, $text, $matches, PREG_OFFSET_CAPTURE); preg_match($pattern, $text, $matches, 0, $matches[0][1]);

Different charset on different server?

回眸只為那壹抹淺笑 提交于 2019-11-29 15:57:46
I've just tested locally my web application, everything works fine, but after uploading to server application behaves differently. I use function formatiraj_string_url to convert diacritic symbols and get clean url... locally it works fine but on server this function doesnt convert them the same way. Few days earlier I tested this on some third server and it worked fine. Now I'm uploading web to test it again on this third server, but I just wonder what could really be the cause of such behavior? function formatiraj_string_url($string) { $string = strtolower($string); $znak[0] = ' '; $znak[1]

Simple: How to replace “all between” with php?

无人久伴 提交于 2019-11-29 09:09:35
$string = "<tag>i dont know what is here</tag>" $string = str_replace("???", "<tag></tag>", $string); echo $string; // <tag></tag> So what code am i looking for? $search = "/[^<tag>](.*)[^<\/tag>]/"; $replace = "your new inner text"; $string = "<tag>i dont know what is here</tag>"; echo preg_replace($search,$replace,$string); outputs: <tag>your new inner text</tag> A generic function: function replace_between($str, $needle_start, $needle_end, $replacement) { $pos = strpos($str, $needle_start); $start = $pos === false ? 0 : $pos + strlen($needle_start); $pos = strpos($str, $needle_end, $start);

php str_ireplace without losing case

落爺英雄遲暮 提交于 2019-11-29 08:00:40
is it possible to run str_ireplace without it destroying the original casing? For instance: $txt = "Hello How Are You"; $a = "are"; $h = "hello"; $txt = str_ireplace($a, "<span style='background-color:#EEEE00'>".$a."</span>", $txt); $txt = str_ireplace($h, "<span style='background-color:#EEEE00'>".$h."</span>", $txt); this all works fine, but the result outputs: [hello] How [are] You instead of: [Hello] How [Are] You (square brackets being the color background) Thanks. You're probably looking for this: $txt = preg_replace("#\\b($a|$h)\\b#i", "<span style='background-color:#EEEE00'>$1</span>",

Remove Unicode Zero Width Space PHP

与世无争的帅哥 提交于 2019-11-29 06:52:57
I have a text in Burmese language, UTF-8. I am using PHP to work with the text. At some point along the way, some ZWSPs have crept in and I would like to remove them. I have tried two different ways of removing the characters, and neither seems to work. First I have tried to use: $newBody = str_replace("​", "", $newBody); to search for the HTML entity and remove it, as this is how it appears under Web Inspector. The spaces don't get removed. I have also tried it as: $newBody = str_replace("&#8203", "", $newBody); and get the same no result. The second method I tried was found on this question

Using str_replace multiple times on the same string

点点圈 提交于 2019-11-29 06:17:19
I'm looping through a title from a table so it's essentially something along these lines. foreach($c as $row){ echo string_shorten($row['title']); } What I'm doing is trying is a switch statement that would switch between what I want it to search for and once it's found replace it with what I choose in the str_replace: function string_shorten($text){ switch(strpos($text, $pos) !== false){ case "Hi": return str_replace('Hi','Hello', $text); break; } } Any suggestions or possible alternatives would be appreciated. It feels like I'm really close but not quite. As you can read in the manual for

Removing Dollar and comma from string

点点圈 提交于 2019-11-29 02:01:35
How can we remove dollar sign ($) and all comma(,) from same string? Would it be better to avoid regex? String liveprice = "$123,456.78"; do like this NumberFormat format = NumberFormat.getCurrencyInstance(); Number number = format.parse("$123,456.78"); System.out.println(number.toString()); output 123456.78 Try, String liveprice = "$123,456.78"; String newStr = liveprice.replaceAll("[$,]", ""); replaceAll uses regex, to avoid regex than try with consecutive replace method. String liveprice = "$1,23,456.78"; String newStr = liveprice.replace("$", "").replace(",", ""); Just use Replace instead

How to remove accents and tilde in a C++ std::string

£可爱£侵袭症+ 提交于 2019-11-29 01:34:41
I have a problem with a string in C++ which has several words in Spanish. This means that I have a lot of words with accents and tildes. I want to replace them for their not accented counterparts. Example: I want to replace this word: "había" for habia. I tried replace it directly but with replace method of string class but I could not get that to work. I'm using this code: for (it= dictionary.begin(); it != dictionary.end(); it++) { strMine=(it->first); found=toReplace.find_first_of(strMine); while (found!=std::string::npos) { strAux=(it->second); toReplace.erase(found,strMine.length());

PHP str_replace replace spaces with underscores

烂漫一生 提交于 2019-11-28 20:50:02
问题 Is there a reason that I'm not seeing, why this doesn't work? $string = $someLongUserGeneratedString; $replaced = str_replace(' ', '_', $string); echo $replaced; The output still includes spaces... Any ideas would be awesome 回答1: I'll suggest that you use this as it will check for both single and multiple occurrence of white space (as suggested by Lucas Green). $journalName = preg_replace('/\s+/', '_', $journalName); instead of: $journalName = str_replace(' ', '_', $journalName); 回答2: Try

ASP.net c# replace string not working

主宰稳场 提交于 2019-11-28 14:28:26
// Build email link confirmLink = Master.siteDomain + "/newsLetter.aspx?action=confirm&e=" + emailAddress + "&code=" + verCode; using (SqlCommand cmd = new SqlCommand("SELECT newRegEmailBody, newRegEmailSubj FROM tblSiteSettings WHERE (isActive = 1)", Master.cn)) { SqlDataReader rdr = cmd.ExecuteReader(); if (rdr.Read()) { emailBody = rdr[0].ToString(); emailSubj = rdr[1].ToString(); } rdr.Close(); } emailBody.Replace("[CONFIRMATION_LINK]", confirmLink); emailer.sendEmail(emailAddress, Master.noReplyEmail, emailSubj, emailBody); It all seems to work fine, except the body is still showing up