Support

Account

Home Forums Front-end Issues Remove 'http://' from text fields

Solving

Remove 'http://' from text fields

  • Hi!

    I’ve got a number of listings with a text field that is a website that has been entered by our users. Some are full URLs (ie: http://site.com/) and some are simple ones (ie: site.com)
    What I need to do is show the website’s URL, but without the “http://” or trailing “/” if they exist.
    Is it possible to capture and filter out parts of a text field??

    Thanks!

    • Daron

    • September 23, 2014 at 1:48 pm

    Using the get_field(); function you can store data in a variable.
    http://www.advancedcustomfields.com/resources/get_field/

    So let’s say the text box value is “http://example.com/” stored in a field called ‘url’.

    First, we grab this value.
    $site = get_field('url');

    Then replace the http:// or https://
    $site = str_replace(array('http://', 'https://'), '', $site);

    Then we remove the trailing slash.
    $site = rtrim($site, '/');

    Finally, echo the value.
    echo $site;

  • This really helped me Daron, thanks for the post!

    • pagol

    • March 12, 2019 at 9:53 pm

    can you please share final code. i want to do similar thing also.

  • Hi, mine was:

    <?php if( get_field('personal_website_or_blog_url') ): ?>
    <a href="http://<?php $site = get_field('personal_website_or_blog_url');
    $site = str_replace(array('http://', 'https://'), '', $site);
    $site = rtrim($site, '/');
    echo $site;?>" data-avia-related-tooltip="view website" target="_blank">
    <i class="fa fa-television" title="Personal Website" aria-hidden="true"></i></a>
    <?php endif; ?> 
    

    … obviously my field was ‘personal_website_or_blog_url’ so you would need to change that out for yours.

    • pagol

    • March 13, 2019 at 5:01 pm

    thank you so much. it is working nicely

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.