jump to last post 1-3 of 3 discussions (6 posts)

Shortlink Generator Question/Help/Assistance!

  1. jacharless profile image78
    jacharlessposted 4 years ago

    Hey Techs,
    Have a question for you.

    Am building a system [using PHP] where a users Long URL is saved table-side at registration. Need to echo a short link on page both for encoding and sizing reasons.

    Have been able to generate a string of characters (8) to use as the "short link" but when I tried to use a pre_replace to swap the Long URL for the Short URL I received a delimiter/alphanumeric character error.

    Any idea how to fix this or an alternative solution. Thanks in advance.  James.

  2. paradigmsearch profile image85
    paradigmsearchposted 4 years ago

    Just some thoughts...

    Sometimes the compiler is a wuss and it's not the programmer's fault. I've solved that problem in the past by:

    1. De-nesting.

    2. Using a string of 2 or 3 simpler commands/functions to replace the single "stronger" command/function.

  3. jacharless profile image78
    jacharlessposted 4 years ago

    Para!
    Right, I did the following:

    $long="gitara.me/Artist/$ARTIST/";

    function generateRandomString($length) {
    $result = ''; for ($i = 0; $i < $length; $i++) {
    $num = rand(48, 122); $result .= chr($num);} return $result;} $short=generateRandomString(8);

    $shortlink=preg_replace($long,"",$short);

    Have no idea why this delimiter error popped up. I checked php.co.uk but got noting back.
    James.

    1. paradigmsearch profile image85
      paradigmsearchposted 4 years ago in reply to this

      I'm not familiar with this language syntax, but I noticed that:

      You have an odd number of "   Do they need to be evenly paired?

      Just a shot in the dark here. smile

      1. jacharless profile image78
        jacharlessposted 4 years ago in reply to this

        No worries.
        The syntax is PHP

        The .= is used in place of a= a . b which is used to cluster elements of a string.
        In this case, Part One is $result=''; or null.
        Part two is $result .= chr($num) which returns is a string of random ASCII characters $num=rand() for~each instance where $result= null.

        So, it states $result = null . chr($num) or $result .= chr($num)
        The last bit assigns a total character output of (8).

        The goal is create a readable shortlink, like bit.ly, fb.me, ow.ly or t.co and allow the visitor to use the shortlink to share the page. I worked out a temp solution which does this command at registration, and stores the shortlink in the database. At runtime, the shortlink is echoed on-page. But, this means loading another script to parse the URI when the domain is opened, and redirect to the true page else a custom 404. Ugh!

        James.

        ps, yes, magic quotes in PHP must always be paired, or it throws errors.

        1. paradigmsearch profile image85
          paradigmsearchposted 4 years ago in reply to this

          I truly am obsolete. But I'm fine with that. big_smile

 
working