WordPress.org

Ready to get started?Download WordPress

Forums

str_replace on variable for $post->post_title (10 posts)

  1. Ryan Paul
    Member
    Posted 4 years ago #

    I currently have 'front-end' delete links on my blog for logged in Administrators with a double javascript check popup.

    In the popup I've got the post title coming up. It works fine if I have an apostrophe in the title, but not if I have speechmarks / quotation marks

    I'm currently using the following:

    $variable = $post->post_title;
    $variable = str_replace(array("'",""), "\'", $variable);

    Adding the following to cater for speechmarks does not work:

    $variable = str_replace(array('"',''), '\\"', $variable);

    Can anyone help me please?

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    Am I missing something? Why can't you just use $variable = str_replace( '"', '\\"' ,$variable);

  3. Ryan Paul
    Member
    Posted 4 years ago #

    It doesn't fix it. When there's speechmarks in the title, it screws up the html.

    I thought it would work too.

  4. Ryan Paul
    Member
    Posted 4 years ago #

    ANYONE DAMNIT?

  5. Ryan Paul
    Member
    Posted 4 years ago #

    Someone?

  6. vtxyzzy
    Member
    Posted 4 years ago #

    Can you give me an example of what you want to happen? esmi's suggestion will replace quotation marks with slash-quotation marks.

    Start string: mac's "title"
    Result string: mac\'s \"title\"

    Is that not what you want?

  7. Ryan Paul
    Member
    Posted 4 years ago #

    That doesn't work. I currently have this:

    $variable = $post->post_title;
    $variable = str_replace( "'", "\'", $variable);
    $variable = str_replace( '"', '\\"', $variable);
    
    	$link = "<a onclick=\"return checkStuff('$variable');\" href='" . wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID) . "'>".$link."</a>";

    I added the following title - Title 1 " Title 2 "

    and the html for the delete link it gave me was this:

    <a onclick="return checkStuff('Test 1 \" test="" 2="" \="" );="" href="URL HERE">Delete</a>

    but when I add the following title: Title 1 ' Title 2' it gives me the following correctly commented html:

    <a onclick="return checkStuff('Title 1 \' Title 2 \' ')" href="URL HERE">Delete</a>

  8. Mark / t31os
    Moderator
    Posted 4 years ago #

    Is using the_title filter an option?

    $yourvar = apply_filters( 'the_title' , $post->post_title );
  9. vtxyzzy
    Member
    Posted 4 years ago #

    I can't explain it unless $post->post_title doesn't contain what you think it does. Maybe it is getting changed somehow before you retrieve it. The str_replace suggested by esmi works as advertised for me. Have you dumped out $variable before the replaces to be sure what it contains?

  10. ouija
    Member
    Posted 3 years ago #

    Little late to post maybe, but did you try using htmlspecialchars?

    http://php.net/manual/en/function.htmlspecialchars.php

Topic Closed

This topic has been closed to new replies.

About this Topic