/    Sign up×
Community /Pin to ProfileBookmark

Putting period at the end of a string

Hello All,

I am still making the transition from Perl to PHP and need some help…

This line used to work fine for me in Perl:

[code=php]$msg .= ‘.’ if $msg !~ /[.!?)(]$/;[/code]

How can I tell it that if the last character is a “.”, “!”, “?”, “)”, or “(” then a “.” should NOT be added to the end?

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@pyroAug 18.2004 — Something like this should do it:

[code=php]<?PHP
$msg = 'This is a test';
if (!preg_match('/[.!?()]$/', $msg)) {
$msg .= '.';
}
echo $msg;
?>[/code]
Copy linkTweet thisAlerts:
@jimmybeachesauthorAug 18.2004 — Thanks pyro, that did it!

So preg_match starts at the beginning of a string and !preg_match starts at the end? If so, does !preg_match know to only look at one character?
Copy linkTweet thisAlerts:
@pyroAug 18.2004 — Nope, actually the ! is inverting the [font=monospace]if[/font]. Rather than proceeding if it does match, it proceeds if it does not match. Not sure how familiar you are with regex, but this is where the magic is worked: [font=monospace]/[.!?()]$/[/font]. The /'s are the delimiters, the [ and ] signify the beginning and end of the character set, the characters inside are what is matched, and the $ at the end tells it to match at the end of the string. Basically, we are just checking if any of the characters inside the character set are the last character.
Copy linkTweet thisAlerts:
@jimmybeachesauthorAug 18.2004 — Wow, thanks for the detailed explanation pyro!

I think this might help me with something else I am trying to do:

All my webpages are in the root web directory (contact.htm, about.htm, etc...)

I would like to be able to send a link to: www.robj.ca/contact/

Then I am trying to have an index.php file in that directory that will dynamically redirect them up one directory to a file with the name of the directory it was in PLUS the .htm extension

Here is what I have so far:

  • 1. In the contact directory is an index.php file that looks at the path were it is


  • 2. Then I am using $_SERVER['PHP_SELF'] to show this: "/contact/index.php"


  • Now I am trying to extract "contact" from between the "/"'s and then redirect to "../$thepage.htm" (contact.htm)

    Does that make sense?
    Copy linkTweet thisAlerts:
    @jimmybeachesauthorAug 18.2004 — maybe there is a simpiler way to say this:

    you browse to www.whatever.com/someting/

    then you want to be redirected to:

    www.whatever.com/something.htm

    then for as many .htm files as you have, you can create a directory with the same name and dump an index.php file into that will rediect the visitor.... I am so lazy ?
    Copy linkTweet thisAlerts:
    @MstrBobAug 18.2004 — Oh, you mean something like:

    [code=php]
    <?php
    $page=$_SERVER['PHP_SELF'];
    $page=preg_replace("//([A-Za-z0-9_-]*?)/(.*?).php/", "$1", $page);
    header("Location: ".$page.".htm");
    ?>
    [/code]


    It will basically pull out the directory that the index.php file resides in, and then send the visitor to a .htm page with the same name. Or am I misunderstanding you?
    Copy linkTweet thisAlerts:
    @jimmybeachesauthorAug 18.2004 — Holy *&$%, that is a little more complicated than that I did:

    <i>
    </i>&lt;?php

    // extension to use:
    $ext = '.htm';

    $script = $_SERVER['PHP_SELF'];

    $thedir = explode("/", $script);

    $redirect = $thedir[1] . $ext;

    header("Location: ../$redirect");

    exit();
    ?&gt;


    or I included an config file to find the right file extension, if they are not all the same:

    the xconfig.inc file:
    <i>
    </i>&lt;?php

    $xtype["passgen"] = ".htm";
    $xtype["pass"] = ".php";

    ?&gt;


    and the index.php file:

    <i>
    </i>&lt;?php

    include("../xconfig.inc");

    $script = $_SERVER['PHP_SELF'];

    $thedir = explode("/", $script);

    $ext = $xtype[$thedir[1]];

    $redirect = $thedir[1] . $ext;

    header("Location: ../$redirect");

    exit();
    ?&gt;


    But I think I will try your solution now ? Thanks!!!
    Copy linkTweet thisAlerts:
    @jimmybeachesauthorAug 18.2004 — When I run your code I get this error:

    [b]

    Warning: Unknown modifier '(' in /home/xxxx/xxxx/xxxx/xxxx/about/index.php on line 3[/b]


    Line 3:

    $page=preg_replace("//([A-Za-z0-9_-]*?)/(.*?).php/", "$1", $page);

    And this error:

    [b]

    Warning: Cannot modify header information - headers already sent by (output started at /home/xxxx/xxxx/xxxx/xxxx/about/index.php:3) in /home/xxxx/xxxx/xxxx/xxxx/about/index.php on line 4

    [/b]


    Line 4:

    header("Location: ".$page.".htm");


    any ideas?
    Copy linkTweet thisAlerts:
    @Stephen_PhilbinAug 18.2004 — Yeah I was gonna say, if all ya wanna do is tinker with certain directory names in the URI then just explode the path to the file at "/"

    So just explode($URI, "/")

    Then re-assemble the URI with another script that will handle the directory names as you wish. Just remember that whichever character you "explode" a string by will be romoved from the resulting arrays contents so say you had :[code=php]<?
    $uri = "one/two/three/four/file.txt";
    $boom = explode($uri, "/");
    /*This will explode the path into it's component parts, but will not work as you might imagine when reassembled. Straight foward reassembly would result in the following echo.... */

    echo $boom['1'] . $boom['2'] . $boom['3'] . $boom['4'] . $boom['5'];

    /*Giving the following output......... */

    "onetwothreefourfile.txt"
    [/code]

    As you can see the URI is now pointing to a single directory called onetwothreefour. So don't forget to put yer "/"'s back in.
    Copy linkTweet thisAlerts:
    @jimmybeachesauthorAug 18.2004 — do you think JavaScript is capable of doing something like this, too?
    Copy linkTweet thisAlerts:
    @Stephen_PhilbinAug 18.2004 — Depends what exactly you want it to do. Remeber javascript can only alter things on the client side and has no effect on the server its self. I wouldn't really rely on js to deliver my users the URI's they need to make a page function though.
    Copy linkTweet thisAlerts:
    @MstrBobAug 18.2004 — Yes, never use Javascript except to COMPLIMENT your page. Never rely on it for anything at all.

    Sorry my code didn't work, these boards play around with and hence, my code was chopped. x.x Mr. Herer's method, of course works. Mine simply grabbed the last directory, no matter the path. Of course if you know the exact path, than you can use Mr. Herer's method. Just for reference, here's mine the right way:

    [code=php]
    <?php
    $page=$_SERVER['PHP_SELF'];
    $page=preg_replace("/\/([A-Za-z0-9_-]*?)\/(.*?).php/", "$1", $page);
    header("Location: ".$page.".htm");
    ?>
    [/code]
    Copy linkTweet thisAlerts:
    @MstrBobAug 18.2004 — Yes, never use Javascript except to COMPLIMENT your page. Never rely on it for anything at all.

    Sorry my code didn't work, these boards play around with and hence, my code was chopped. x.x Mr. Herer's method, of course works. Mine simply grabbed the last directory, no matter the path. Of course if you know the exact path, than you can use Mr. Herer's method. Just for reference, here's mine the right way:

    [code=php]
    <?php
    $page=$_SERVER['PHP_SELF'];
    $page=preg_replace("/\/([A-Za-z0-9_-]*?)\/(.*?).php/", "$1", $page);
    header("Location: ".$page.".htm");
    ?>
    [/code]
    Copy linkTweet thisAlerts:
    @pyroAug 18.2004 — It's all about [font=monospace]mod_rewrite[/font], boys... ?
    ×

    Success!

    Help @jimmybeaches spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...