Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8178265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:43:18+00:00 2026-06-06T23:43:18+00:00

I could not find an optimal solution for this problem. The idea is to

  • 0

I could not find an optimal solution for this problem. The idea is to change all the urls of a text containing a specific domain with a preg_replace_callback() to base 64 codification. The urls are of this type: http://www.domain.com/?fsdf76sf8sf6fds and separately the other way: http://www.otherdomain.com/file/CA60D10F8ACF7CAA

any ideas for the regex?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:43:19+00:00Added an answer on June 6, 2026 at 11:43 pm

    What you are looking for is something along the lines of

    $s = preg_replace_callback('#(([a-z]+://)|([a-z]+://)?[a-z0-9.-]+\.|\b)domain.com[^\s]+#i', function($match) {
        return base64_encode($match[0]);
    }, $string);
    

    That regular expression may be a bit confusing, so let’s break it down:

    (  -- domain.com must be preceeded by either
      ([a-z]+://)   -- a protocol such as http://
      |
      ([a-z]+://)?[a-z0-9.-]+\.  -- possibly a protocol and definitely a subdomain
      |
      \b  -- word-break (prevents otherdomain.com from matching!)
    )
    domain.com  -- the actual domain you're looking for
    [^\s]+  -- everything up to the next space (to include path, query string, fragment)
    

    A very simple system to test stuff like this:

    <?php
    
    $strings = array(
        // positives
        'a http://www.domain.com/?fsdf76sf8sf6fds z' => 'a xxx z',
        'a www.domain.com/?fsdf76sf8sf6fds z' => 'a xxx z',
        'a http://domain.com/?fsdf76sf8sf6fds z' => 'a xxx z',
        'a domain.com/?fsdf76sf8sf6fds z' => 'a xxx z',
        // negatives
        'a http://www.otherdomain.com/file/CA60D10F8ACF7CAA z' => null,
        'a www.otherdomain.com/file/CA60D10F8ACF7CAA z' => null,
        'a http://otherdomain.com/file/CA60D10F8ACF7CAA z' => null,
        'a otherdomain.com/file/CA60D10F8ACF7CAA z' => null,
    );
    
    foreach ($strings as $string => $result) {
        $s = preg_replace_callback('#(([a-z]+://)|([a-z]+://)?[a-z0-9.-]+\.|\b)domain.com[^\s]+#i', function($match) {
            return 'xxx';
        }, $string);
    
        if (!$result) {
            $result = $string;
        }
    
        if ($s != $result) {
            echo "FAILED: '$string' got '$s'\n";
        } else {
            echo "OK: '$string'\n";
        }
    }
    

    (should you already be unit testing, use that instead, obviously…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could not find a solution to what seems to me as a simple problem.
I could not find a clear answer to this question elsewhere, so I'll try
I have searched but could not find the reason for this behavior. I have
I've been searching around but could not find any answers to this. What I'm
I have been looking everywhere but could not find a tutorial for this. I
Could not find a clear and recent explanation of how to achieve this. Does
Could not find this through Google or in SO questions... I have a checkbox
I could not find any clear statement on MSDN regarding this, there are some
could not find anything on this, wondering if anyone knew about this or a
I could not find the exact question answered on this site. The html on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.