Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
2Fast2Nutricious
Oct 4, 2020

Like people said, array_map('trim', ...) is the most appropriate way to go.


Mind_Taker posted:

I'd just do a regex replace using preg_replace to get it done in a single line of code.

The regex you write to make sure you're not replacing multiple spaces in between characters would be a bit unwieldy, no?

Adbot
ADBOT LOVES YOU

kiwid
Sep 30, 2013

2Fast2Nutricious posted:

The regex you write to make sure you're not replacing multiple spaces in between characters would be a bit unwieldy, no?

Nah, easy with a positive lookbehind.

PHP code:
preg_replace('/(?<=[[:blank:]])[[:blank:]]+/', '', $string)
https://regex101.com/r/FMcUXb/4

Or if you don't want the leading space either:

PHP code:
preg_replace('/(?<=\s)\s+/', '', $string)
https://regex101.com/r/FMcUXb/5



kiwid fucked around with this message at 20:08 on Sep 27, 2023

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

kiwid posted:

Nah, easy with a positive lookbehind.

PHP code:
preg_replace('/(?<=[[:blank:]])[[:blank:]]+/', '', $string)
https://regex101.com/r/FMcUXb/4

Or if you don't want the leading space either:

PHP code:
preg_replace('/(?<=\s)\s+/', '', $string)
https://regex101.com/r/FMcUXb/5

If you wanted to do this the easy way would be to just replace multiple spaces with a single space, instead of replacing all-but-one of the spaces with nothing.

But you're also missing the point, because the original question doesn't want to collapse multiple spaces if they're in the middle of an entry instead of at the start or end.

Mind_Taker
May 7, 2007



I hate looping over strings by line so I'd do:

php:
<?php

$file_contents file_get_contents($filename);

$new_file_contents preg_replace('/(^\s+)|(\s+$)/'''preg_replace('/\s*\|\s*/''|'$file_contents));

Basically replace a pipe with spaces on either side of them with just a pipe. And then also replace beginning and ending spaces on a line with the empty string.

Mind_Taker fucked around with this message at 05:05 on Sep 28, 2023

putin is a cunt
Apr 5, 2007

BOY DO I SURE ENJOY TRASH. THERE'S NOTHING MORE I LOVE THAN TO SIT DOWN IN FRONT OF THE BIG SCREEN AND EAT A BIIIIG STEAMY BOWL OF SHIT. WARNER BROS CAN COME OVER TO MY HOUSE AND ASSFUCK MY MOM WHILE I WATCH AND I WOULD CERTIFY IT FRESH, NO QUESTION
Cross posting here from the WordPress thread since it's so slow moving:

I am trying to set up a WordPress container for local development. I'm using the bitnami/wordpress image and in my Dockerfile I'm copying my theme files into the image at /wordpress_themes/my_theme and I have an init script that will symlink the contents of that directory to the /bitnami/wordpress/wp-content/themes/my_theme directory, and then set the WordPress theme to that them using wp-cli.

This all works great. I can build an image that holds everything and works well, and I can also run the container with my theme directory mounted as a volume at /wordpress_themes/my_theme and my local changes will show up in the container. BUT there's always a delay, about 5 minutes. I assume something in WordPress is doing some caching of the template files, but I can't work out what it is doing and how to disable this behaviour. The changes show up just fine, it all works really nicely, but it just takes too long for the changes to show.

Agrikk
Oct 17, 2003

Take care with that! We have not fully ascertained its function, and the ticking is accelerating.
What is the best way to accomplish this programmatically?



Given a wealth level "poor, normal, rich, etc." I can RAND(1,100) to select the number of items using a SWITCH statement. Right now I solve this by creating five switch statements, one for each column, and then wrap them in an additional switch statement for the wealth level.

Is that it?

Intuitively, it seems that this can be done somehow using a multidimensional array but I can't get my head around it.

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


That's probably the simplest way. If by multidimensional, you mean have it return an array that you key off of, it'd probably be like below.

php:
<?php
// If rolled a 92, this returns a 3, etc
ItemComponent::roll()[ItemComponent::NORMAL];

php:
<?php

class ItemComponent
{
    public const VERY_POOR "Very Poor";
    public const POOR "Poor";
    public const NORMAL "Normal";
    public const RICH "Rich";
    public const VERY_RICH "Very Rich";

    public static function roll(): array
    {
        $roll rand(1100);
        return match (true) {
            in_array($rollrange(120)) => [
                ItemComponent::VERY_POOR => 0,
                ItemComponent::POOR => 0,
                ItemComponent::NORMAL => 0,
                ItemComponent::RICH => 0,
                ItemComponent::VERY_RICH => 0,
            ],
            in_array($rollrange(2140)) => [
                ItemComponent::VERY_POOR => 0,
                ItemComponent::POOR => 0,
                ItemComponent::NORMAL => 0,
                ItemComponent::RICH => 1,
                ItemComponent::VERY_RICH => 1,
            ],
            in_array($rollrange(4155)) => [
                ItemComponent::VERY_POOR => 0,
                ItemComponent::POOR => 0,
                ItemComponent::NORMAL => 1,
                ItemComponent::RICH => 2,
                ItemComponent::VERY_RICH => 2,
            ],
            in_array($rollrange(5670)) => [
                ItemComponent::VERY_POOR => 0,
                ItemComponent::POOR => 1,
                ItemComponent::NORMAL => 1,
                ItemComponent::RICH => 2,
                ItemComponent::VERY_RICH => 3,
            ],
            in_array($rollrange(7180)) => [
                ItemComponent::VERY_POOR => 0,
                ItemComponent::POOR => 1,
                ItemComponent::NORMAL => 2,
                ItemComponent::RICH => 2,
                ItemComponent::VERY_RICH => 4,
            ],
            in_array($rollrange(8190)) => [
                ItemComponent::VERY_POOR => 1,
                ItemComponent::POOR => 1,
                ItemComponent::NORMAL => 2,
                ItemComponent::RICH => 3,
                ItemComponent::VERY_RICH => 5,
            ],
            in_array($rollrange(9194)) => [
                ItemComponent::VERY_POOR => 1,
                ItemComponent::POOR => 2,
                ItemComponent::NORMAL => 3,
                ItemComponent::RICH => 3,
                ItemComponent::VERY_RICH => 6,
            ],
            in_array($rollrange(9597)) => [
                ItemComponent::VERY_POOR => 2,
                ItemComponent::POOR => 3,
                ItemComponent::NORMAL => 4,
                ItemComponent::RICH => 4,
                ItemComponent::VERY_RICH => 7,
            ],
            in_array($rollrange(9899)) => [
                ItemComponent::VERY_POOR => 3,
                ItemComponent::POOR => 4,
                ItemComponent::NORMAL => 5,
                ItemComponent::RICH => 6,
                ItemComponent::VERY_RICH => 8,
            ],
            $roll === 100 => [
                ItemComponent::VERY_POOR => 4,
                ItemComponent::POOR => 5,
                ItemComponent::NORMAL => 6,
                ItemComponent::RICH => 8,
                ItemComponent::VERY_RICH => 10,
            ],
        };
    }
}


Agrikk
Oct 17, 2003

Take care with that! We have not fully ascertained its function, and the ticking is accelerating.
Thanks for confirming what I was thinking: that a series of switch statements was the play here.

But your function looks really cool and I'll keep it for future use. Thanks!

nielsm
Jun 1, 2009



You can also structure it as a lookup table, where each item stores the range start, range end, and the mapped values, then search the mapping by value. Or if all the search values are integer, use an array with the lookup key, so e.g. indexes 1 up to 20 in the lookup array all reference the same mapping object. If you need to translate a large number of values that's probably an advantage performance-wise.

Zamujasa
Oct 27, 2010



Bread Liar

Agrikk posted:

What is the best way to accomplish this programmatically?



Given a wealth level "poor, normal, rich, etc." I can RAND(1,100) to select the number of items using a SWITCH statement. Right now I solve this by creating five switch statements, one for each column, and then wrap them in an additional switch statement for the wealth level.

Is that it?

Intuitively, it seems that this can be done somehow using a multidimensional array but I can't get my head around it.

it can be:

php:
<?
    function fart($roll, $wealth) {
        $rolls = [
            20 => [0, 0, 0, 0, 0],
            40 => [0, 0, 0, 1, 1],
            55 => [0, 0, 1, 2, 2],
        
            70 => [0, 1, 1, 2, 3],
            80 => [0, 1, 2, 2, 4],
            90 => [1, 1, 2, 3, 5],
            94 => [1, 2, 3, 3, 6],

            97 => [2, 3, 4, 4, 7],
            99 => [3, 4, 5, 6, 8],
            100 => [4, 5, 6, 8, 10],
        ];

        foreach ($rolls as $max => $table) {
            if ($roll <= $max) {
                return $table[$wealth];
            }
        }
    }
?>
this works because arrays in php keep their sort order. the index/key of the array is the highest dice roll, and then the value is an array of the wealth levels.
it basically asks: "is the roll lower than 20? well, what about 40? what about 60?" etc. until it gets an answer, and then it returns the wealth level of that particular entry.

the expected inputs are 1-100 for the roll and 0-4 for the wealth level, but you can make them anything.
fart( 1 ~ 100, 0 ~ 4 )

edit: this is basically an implementation of the above poster's thought, but without the "minimum" range, since the minimums are always either infinity or the previous level's maximum.

edit 2: i would argue one benefit of the above (with the proper formatting) is that it preserves a very clear 1:1 relationship with the original chart. if you saw the chart and the code side by side it's immediately obvious what the data is and how it should work (though of course if you're using it somewhere serious please add a few comments)

Zamujasa fucked around with this message at 22:53 on Dec 29, 2023

2Fast2Nutricious
Oct 4, 2020

Maybe even this

code:

function pfft(int $roll, int $wealth): int {
    $range = fn ($max) => $roll <= $max;

    return (match(true) {
        $range(20) => [0, 0, 0, 0, 0], 
        $range(40) => [0, 0, 0, 1, 1], 
        $range(55) => [0, 0, 1, 2, 2], 
        $range(70) => [0, 1, 1, 2, 3], 
        $range(80) => [0, 1, 2, 2, 4], 
        $range(90) => [1, 1, 2, 3, 5], 
        $range(94) => [1, 2, 3, 3, 6], 
        $range(97) => [2, 3, 4, 4, 7], 
        $range(99) => [3, 4, 5, 6, 8], 
        $range(100)=> [4, 5, 6, 8,10]
    })[$wealth];
}
and this is why I get paid the big bucks :q:

code:

$my_eyes = fn(int $roll, int $wealth): int =>
 [
        [0, 0, 0, 0, 0], 
        [0, 0, 0, 1, 1], 
        [0, 0, 1, 2, 2], 
        [0, 1, 1, 2, 3], 
        [0, 1, 2, 2, 4], 
        [1, 1, 2, 3, 5], 
        [1, 2, 3, 3, 6], 
        [2, 3, 4, 4, 7], 
        [3, 4, 5, 6, 8], 
        [4, 5, 6, 8, 10]
    ][array_sum([
        $roll > 20,
        $roll > 40,
        $roll > 55,
        $roll > 70,
        $roll > 80,
        $roll > 90,
        $roll > 94,
        $roll > 97,
        $roll > 99,
    ])][$wealth];

Zamujasa
Oct 27, 2010



Bread Liar
it's like code golf but you're trying to hit the ball through windows :allears:

Agrikk
Oct 17, 2003

Take care with that! We have not fully ascertained its function, and the ticking is accelerating.
I have the following MySQL query:

SQL code:
(SELECT item, data1, data2 FROM table WHERE item = XX)
UNION
(SELECT item, data1, data2 FROM table WHERE item < XX ORDER BY item DESC LIMIT YY)
UNION
(SELECT item, data1, data2 FROM table WHERE item > XX ORDER BY item ASC LIMIT YY)
How do I write the query in PHP and the associated bind_param statement?
Do I have to substitute each XX and YY and have five question marks and five entries in the bind_param statement?

spiritual bypass
Feb 19, 2008

Grimey Drawer
Yes, that's how you'd write the SQL. It's better to prepare a statement with PDO (the builtin database abstraction) and execute it with your arguments than to use bind_param (is that libmysql?)

kiwid
Sep 30, 2013

There is an open source package that I want to use to drastically reduce the manual work I'd need to do in my database queries. Unfortunately, the package only supports drivers for MySQL, Postgres, and SQLite and my application uses SQL Server. I see that there is a pull request on the repo that implements an SQL Server driver and commenters have confirmed it to be working but the PR has gotten no attention from the maintainer of the package for some reason.

I suspect the maintainer is just busy and will one day merge the PR but I can't wait for that. What is the best way to install the package with the SQL Server driver merged? Do I fork the repo, merge the PR, then install my forked repo until the maintainer one day merges it into his own or is there another way that I'm not thinking of?

musclecoder
Oct 23, 2006

I'm all about meeting girls. I'm all about meeting guys.
That's what I'd do, assuming you can have Packagist point to your forked repository (or I guess have Composer install it directly). Short of that, you could use the official package and then copy the code from the PR to your codebase until it's merged.

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


If you're using composer to manage your dependencies, you can override a package with a version from a repo you set in the composer file.
https://getcomposer.org/doc/05-repositories.md#vcs

kiwid
Sep 30, 2013

musclecoder posted:

then copy the code from the PR to your codebase until it's merged.

I hate doing this because the code would need to be in the vendor folder which I might forget about and accidentally wipe out.

duz posted:

If you're using composer to manage your dependencies, you can override a package with a version from a repo you set in the composer file.
https://getcomposer.org/doc/05-repositories.md#vcs

This worked perfectly.

nielsm
Jun 1, 2009



You can clone the original repository locally and checkout the relevant branch or tag. Then add the other repository that the Pull Request is sent from as a second remote to your local checkout, and fetch it. Then you should have the branch the pull request is from as a reference that you can merge in.

So it becomes something like:
code:
git clone https://github.com/Vendor/Product
cd Product
git checkout release-branch
git remote add contributor https://github.com/contributor/Product
git fetch contributor
git merge contributor/feature-branch

Glimm
Jul 27, 2005

Time is only gonna pass you by

Is there a preferred package for viewing Laravel logs as an admin on a site?

We're hosted in Azure, and viewing the logs through the Azure interface kinda stinks

kiwid
Sep 30, 2013

Anyone know a good video to teach me reflection? It's all magic to me and it's about time I learn how it actually works.

kiwid
Sep 30, 2013

Glimm posted:

Is there a preferred package for viewing Laravel logs as an admin on a site?

We're hosted in Azure, and viewing the logs through the Azure interface kinda stinks

Never used it but this is what Laravel Forge integrates with: https://www.papertrail.com/

spiritual bypass
Feb 19, 2008

Grimey Drawer

kiwid posted:

Anyone know a good video to teach me reflection? It's all magic to me and it's about time I learn how it actually works.

No, but I'm curious where you need/want it

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


The only place I use it anymore is in testing to make private methods public. All other uses of it have been taken over by the framework doing it for me.

Adbot
ADBOT LOVES YOU

kiwid
Sep 30, 2013

spiritual bypass posted:

No, but I'm curious where you need/want it

Was going to try building a simple framework to further my skills. Not actually to use.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply