Monks, I'm stumped. I'm using the following to take a record title and create a filename from the title. It works but if the title has more than 8 words the filename ends up being short, and without the .html. I cannot seem to find where this is breaking down. If the title is shorter than 8 words it works fine, but 9 words and it fails.

The filename should be something like: 2018-09-25-this-is-a-sample-title.html.

I also do some checking to see if the filename is already used and add a "2" to the end of the filename.

$base_title = $title; $base_title =~ s/[^\w| ]/ /g; #Remove non-alphanumeric characters a +nd replace them with spaces $base_title =~ s/ {2,}/ /g; #remove multiple spaces $add_date = $dateadded; @words = split (/ /, $base_title); $filename = $dateadded."-" . join("-",@words) . ".html"; $filename_base = $filename; $filename_base =~ s/\.html//g; # check to see if filename is being used $SQL = "SELECT * FROM $database_table WHERE filename = '$filename' +"; &Do_SQL; $i=2; while ($pointer = $sth->fetchrow_hashref){ $filename = $filename_base."$i.html"; $i++; $SQL = "SELECT * FROM $database_table filename = '$filename'"; &Do_SQL; } $SQL = "INSERT INTO $database_table (filename) VALUES ('fi +lename')"; &Do_SQL;

Any help would be appreciated. It might be simple, but I'm stumped. Thanks!


In reply to Creating filenames from title by htmanning

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.