PHP - I Need To Replace Certain Characters
Similar TutorialsI display file names by this simple code echo $path_parts['filename'], "\n"; 1. How can I replace some characters, e.g. replacing "_" with " " (space)? 2. How can I capitalize the output words? Hi, I believe this is my first post on here. First off let me say thank you for silently helping out in the past while I keep on learning PHP. I have decided to build an image gallery, and I am having problems with the page numbering links and the breadcrumbs. Reson I decided to build my own is that I wanted a challange to learn more. I could not find anything that would intregrate with the site as I wanted it to (pulled as a function so offers some customising with options). The problem that I am having is that the <a href= tag is starting to give multipul "/"s when I only need one, so that the url at the top is coming up as http://localhost/mmv4/gallery/pics///Ax/Ax_Jo/ when I am wanting http://localhost/mmv4/gallery/pics/Ax/Ax_Jo/IMG_4244.JPG. I have used str_replace to catch //s and more, but this then adds odd slashes on the end so the page numbeing is getting odd results. some dirs have the / on the end and some do not, so the links are sometimes "visited" and others not. I beleve that the problem is generated in Code: [Select] <?php if(isset($_GET['album'])) { //load the available dirs into an array so that the sneeky users cannot get further back. $files = scandir($gal); $dirs = array(); foreach($files as $file){ if($file != '.' && $file != '..' && $file != 'thumbs') { if(is_dir($gal."/".$file)) { $dirs[] = $file; //echo $file ."<br />\n"; } } } //if(in_array($_GET['album'],$dirs)) { $album = "/".$_GET['album']."/"; $album = str_replace("..", "", $album); //} //else { // $output .= "<p>There is no album called \"$_GET[album]\", please select an album from below.</p>"; //} } or the breadcrumbs bit Code: [Select] <?php if($crumbs == 1) { //bread crums $path = $gal.$album; $path = explode("/", $path); $crumbs = array(); foreach($path as $key => $value) { if($value != ""){ $crumbs[] = $value; } } // create Breadcrumbs div $output .= "<div id=\"breadcrumbs\">"; $number = count($crumbs); $real = ""; foreach($crumbs as $num => $link) { if ($link == $gal) { $link = "Gallery Home"; } if ($num == $number -1) { $output .= $link; } else{ if ($num >= 1){ $real =""; for($x=1; $x<=$num; $x++) { $real .= "/".$crumbs[$x]; } $output .= "<a href=\"?album=".str_replace("/", "",$real)."\">".$link."</a>"." <- "; } else{ $output .= "<a href=\"?album=".str_replace("/", "",$real)."\">".$link."</a>"." <- "; } } $num +1; } //close breadcrumbs div $output .= "</div>"; } Can any one please help me shed some light on this one, I am confused. I can send / attach the full source if needed. MOD EDIT: tags fixed for syntax highlighting . . . 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 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']); } First forgive me, I do not know if this is the correct forum for this question. I did not write the code but I have a feeling if its possible to correct it, then its probably coding. My question is, is there anyway to post larger News articles to the homepage. I am trying to post an article that a friend sent from another site onto mine (giving the other site proper credit). But the article is apparently too big. I do not know how many characters the article is, or what the character limit is set by Nuke 8.0. Is there anything that I can do? Thanks..oh and congrats on apparently being the only active PHP forums :-S I have tried a few others and they haven't made posts since 2011. Anyways thanks in advance for any and all help. I want to be able to keep people from entering certain characters in a form. I've tried google, and had no luck so far. Thanks! Hi What code would i use to take the first 2 letters of a post code so for example CM11 2AY I want the CM bit what command would strip the first 2 characters out? Thanks Hi guys! I am learning PHP now and I am enjoying it. Im not an I.T. graduate that is why Im having very difficult time to understand codes. My problem is how to get the last character of a URL that I get using another php code. I can already post the URL on my page but it displays all the URL of the certain page that I get. Example: the URL is "http://mysite.com/page_1/pp1/?lang=zh" I only want to get the "?lang=zh". I am working under 3 languages and I want to get only that last part of the URL for me to continue my work. I dont exactly know what string or filtering I will do to get that part only. Please help me guys. I will appreciate all your comments here. alright, is this the correct format for a REPLACE INTO? if not what do i need to change.......it isn't working...... Code: [Select] $sql = "REPLACE INTO Events (name, event, eventid, userid, title1, title2, title3, title4, title5, title6, title7, title8, email) VALUES('$name','$event','$eventid','$userid','$title1','$title2','$title3','$title4','$title5','$title6','$title7','$title8', '$email')"; $rs = mysql_query($sql) or die ("Problem with the query: $sql<br>" . mysql_error()); How does wordpress and other similar CMS/applications replace their div elements? For example, if you view the source code of a wordpress site, you will only see: Quote <div id="header"></div> Where, the above code is clearing being switched out for "header.php". How is this done? I believe it's php and jquery, but I would be wrong. Thanks! Hello: I have a quick question. I am currently using the following code to replace an apostrophe and quote marks when inserting and updating a database record: Code: [Select] $myPageContent = mysql_real_escape_string(str_replace("'", "'", $_POST['myPageContent'])); $myPageContent = mysql_real_escape_string(str_replace("", """, $_POST['myPageContent'])); My question is - how can I modify it so I only need to use one line. Like (but this does not work): Code: [Select] $myPageContent = mysql_real_escape_string(str_replace("'", "'", "", """, $_POST['myPageContent'])); It seems to me that the way I am currently doing it might be POSTing the data twice? Thanks! User can edit a post. I need to replace the html '<br/>' element to newline character ('\n') . When a user will edit a post he/she will not see the '<br/>'. How can i do this with php ? ok so I have an string with data that I need to be replaced with contents from an array. I need the occurance position replace with the index of the array. example Code: [Select] //here is the setup $string = "testing %s one %d %s"; array('this',1,'three'); //here is the result testing this one 2 three the problem I am having is because the %s can occur more than once I cant use str_replace. I am working on a solution with substr_replace but was wondering if anyone had any suggestions on how to do this. When I replace it I also need to run the value through a function so keep that in mind. Also the replace's are stored in an array so they need to be looped through as well. Code: [Select] $replaces = array('%s'=>'func_one','%d'=>'func_two'); Any help is appreciated. Hi, I'm pretty new to PHP, and I'm trying to write my own encoding/decoding script (not with base_64). I'd like to replace like all A's with B's, all B's with C's, C's with D's, and so on. How would I do this? Here's an example: String #1: "ABCDEFG" string #1 goes through my encryption string #2 (output): "BCDEFGH". How would I go about doing this? Thanks. I retrieve urls that have the following path $url=http://cdn.test1.com $url=http://cdn.test2.com and so on... i want to to do a string replace such that it displays http://test.com,http://test2.com and son without the cdn part. Will the following work for all cases? Code: [Select] $url=str_replace("cdn.","",$url); I have multiple \n in a string, and I need to replace it with <br />. How do I go about doing this? Preg replace? Hello, I'm trying to convert a paragraph of text which is already html formatted. I wish to find keywords, such as, 'PHP', which are contained in the paragraph, and replace them with a link, for example <a href="http:/www...">PHP</a> str_replace('PHP', '<A href=""....">PHP</a>', $content); My problem arises when some of these keywords are already contained in a link, so say, visit PHP website, is all one link, so str_replace would simply convert the PHP in this string, to two links for one single word. Code: [Select] <a href="">visit <A href=""....">PHP</a> website</a>How could this problem be resolved? Thanks $name = "D'Angelo" ok I'm running a mysql query as $query = " INSERT INTO TEST (ID, NAME) VALUES ('NULL','$NAME')"; If the name = "D'Angelo" the apostrophe would cause it to fail. Is there a way to do this without striping the characters? |