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 8750029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:47:38+00:00 2026-06-13T12:47:38+00:00

Here we go again, I need some help with this. the preg_match is not

  • 0

Here we go again, I need some help with this.
the preg_match is not working as I want it, it is not validating any of site links. I need a 2nd pair of eyes to help me see what is wrong with my code.

if (!empty($_POST["url"]))
      {
          if (filter_var($_POST["url"], FILTER_VALIDATE_URL))
          {
              if (!preg_match('/^http(s)?:\/\/(?:[a-z\d-]+\.)*mysite.com(?:(?=\/)|$)/i', $url))
              {
                echo "<strong>Error</strong>: Not a valid Mysite.com link or could shorten link";
              } else {
                  $result = $sql->query("SELECT `id` FROM `shortcuts` WHERE `url`='{$_POST["url"]}'");
                  $id = $result[0]["id"];
                  if (empty($id))
                  {
                      $result = $sql->query("INSERT INTO `shortcuts` (`url`) VALUES ('{$_POST["url"]}')");
                      if ($result)
                      {
                          $id = $sql->get_increment();
                          if (empty($id))
                          {
                              echo "FAILED ENCODE";
                              exit(1);
                          }
                      }
                      $shorturl = "http://mysite.com/".encode($id);
                  }
              }
          }
      }
  • 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-13T12:47:39+00:00Added an answer on June 13, 2026 at 12:47 pm

    Make yourself the code less complicated, so it is easier to debug. You are concerned about a specific problem here if I understood you right:

    if (matches_shorturl_pattern($url) {
        // go on ...
    }
    

    Create yourself a function. Good thing about is, you can test it isolated without submitting something or hitting the database.

    /**
     * is URL a valid shorturl by syntax?
     *
     * @param string $url
     * @return bool
     */
    function matches_shorturl_pattern($url) {
        $pattern = '/^http(s)?:\/\/(?:[a-z\d-]+\.)*mysite.com(?:(?=\/)|$)/i';
        $result = preg_match($pattern, $url);
        if ($result === FALSE) {
            throw new Exception('The regex did a poop.');
        }
        return (bool) $result;
    }
    

    It’s much easier to test this function alone with the inputs you want to test it again. Also you’re properly checking for error conditions then. Good luck.

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

Sidebar

Related Questions

Once again, I need some help and I appreciate you all for being here
Perl newbie here...I had help with this working perl script with some HASH code
So once again I need some help with this program for class. Maybe its
I'll try this again. I need help in getting a third option working using
I need some help with this accordion. Please see the demo here. http://jsfiddle.net/a36RL/ $(document).ready(function()
I need some help with a query (sorry, I'm not too good at this!)
Once again I need some help. I'm working with a table that contains 3
I am again posting the same question. Please help me in this problem. Here
I received some great help here the other day and hope once again I
Help, I am a noob, just need some advice on this bit of code.

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.