Help with replacing form input name blank spaces with underslash.

I have an edit post and create post form, the same form only if it’s to edit, the fields will have the data in the input fields and it will be blank when creating a new post. I want where the input for name to be changed where if there’s blank space to replace with underslash “_”. The form function is as follows :

[php]
// For new post
function newPost() {
$post = new Posts;
$post->storeFormValues( $_POST );
}

// For post edit

function editPost() {
$post->storeFormValues( $_POST );
}

// storeFormValues() function

public function storeFormValues( $params ) {
$this->__construct( $params ); // Store all the parameters
}

// __construct( $params ) function

public function __construct( $data=array() ) {
if ( isset( $data[‘postName’] ) ) $this->postName = preg_replace ( “/[^.,-_’”@?!:blush: a-zA-Z0-9()]/", “”, $data[‘postName’] );
}
[/php]

Help on this will be as always greatly appreciated.

I meant to say underscore. I apologize for that.

http://php.net/manual/en/function.str-replace.php

[php]$d1=$data[‘postName’]
str_replace(" ", “_”, $d1);[/php]

sholld work i didnt test it but try it

Which function do I want to put that in?

@cabalsdemon - I tried using this and it did not work. I’m not sure as to where it should be called. I tried it in all the functions in my post and I tried different approaches yet it did not work. I need more guidance on this. I very much appreciate the help and assistance.

i was telling you how to replace the blanks with a underscore

If I understand correctly, you could accomplish this with your existing preg_replace. preg_replace accepts arrays. For example:

[php]
preg_replace (array("/[^.,-_’"@?!:blush: a-zA-Z0-9()]/", “\s”), array("", “_”), $data[‘postName’] )
[/php]

:)Great work m@tt. I did try something similar to this before as I knew I could do it with the preg replace that was already being used but failed doing so. I hope this isn’t out of topic but I’m using the preg replace () for blank spaces for url reasons, and now for my links I leave the preg replace() to do it’s job, but now my question is when I echo it out on pages to be displayed I do not want the underscore to be shown so I’m using the string replace() to remove it and put back a blank space. I’m able to use the echo string replace () two different ways and they both work.->

[php]
echo (str_replace ( “", " ", $post->postName));
[/php]
and
[php]
echo str_replace ( "
”, " ", $post->postName);
[/php]

The question is which way is the more proper way? Thanks in advance for any input on this.

I don’t understand the reasoning for replacing spaces with underscores then back again. Are you sure this isn’t something you could use urlencode() and urldecode() for? As far as your question, the additional parenthesis are unnecessary, the code is executed exactly the same for both.

More clearly what I meant to say was that when being stored to the database and when linking to the pages by default it has the underscore, but when echoing the post name out for instance in a heading, page title, or within a paragraph I have the str_replace () to remove the default underscores with blank spaces. Is this bad practice or is it fine to do so?

no this is your preference on how you want to do it

so it is fine

Sponsor our Newsletter | Privacy Policy | Terms of Service