Support

Account

Home Forums General Issues String Replace in Content Output Field

Solving

String Replace in Content Output Field

  • Hi folks
    got stuck on this as I need to do a string replace for only one page, which is being output as an old format XML atom feed, so I cannot use the inbuilt WP Atom feed that we thought would work.

    So, we have a custom post box in our WP/CMS to output a news story without certain markup, to remain a clean item. However, for the XML out put (basically using custom RSS/Atom template for custom page), trying to do stuff like this and not getting it right

    Upshot need to output the post field inside custom RSS feed by calling the ACF field, in this case ‘cleanpostbox’ … but when ‘printed’ in the template page, replace the paragraph open/close tags. Will also need to do same for anchor links, but cannot even get this to work right.

    <?php
    $text = get_field('cleanpostbox');
    $stripped_text = str_replace(array('<p>','</p>'),('<apxh:p>','</apxh:p>'),$text);

    print $text;
    ?>

    Tried a bunch of things including custom function targeting the single post page 55813, etc. Tried doing array replacement on the_content and just not getting there. Thought I’d try this forum for the rare ‘hwlp’ before I go hire sonmbody who could likely sort it in three shakes of a lamb’s tail.

    Not a newbie to WP (since 2005), but I don’t code PHP daily. Hence the duh here. TIA!

  • What are you trying to replace and what are you trying to replace it with, it’s unclear by looking at your code.

  • HI, John
    thanks for reply.

    Basically, I need to replace all the paragraph tags with
    apxh:p (opening and closing tags)

    and also replace all the anchor link tags
    with similar custom XHTML ATOM coding.

    So need to do string replace of a 400 word text block for news items, which is in a ACF custom field which we insert same time as doing a ‘dirty’ version of the news in the normal content field (we don’t use Gutenberg).

    Trying to output a custom RSS/atom feed, and in the template call the field, but do string replace on the output as we can’t have [CDATA for the feed, and needs the custom coding on the paragraph and anchor links.

    Hope that made sense.

  • what you have is missing a bit, but it should work

    
    $text = get_field('cleanpostbox');
    $stripped_text = str_replace(array('<p>','</p>'),array('<apxh:p>','</apxh:p>'),$text);
    echo $stripped_text;
    
  • I think it’ll help you
    try it

    Use str_replace() with fewer elements in replace than find:
    I am just giving an example to you with word of hello world

    <?php
    $find = array(“Hello”,”world”);
    $replace = array(“B”);
    $arr = array(“Hello”,”world”,”!”);
    print_r(str_replace($find,$replace,$arr));
    ?>

  • Thanks folks. Will be getting back to this Sat. morning as had some wonky client project while this issue is for my own stuff.

    Wrinkle I ran into is that the ACF field I’m using actually puts double
    <br><br> tags.
    So would need to replace with
    ‘</apxh:p><apxh:p>’

    And also replace
    '<a href>' with <apxh:a href>
    and '</a>' with '</apxh:a>'

    I think I’m going to have to hire an ACF pro on this one!

  • that was helpful thanks.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘String Replace in Content Output Field’ is closed to new replies.