Jump to content

MoFish

Members
  • Posts

    227
  • Joined

  • Last visited

Posts posted by MoFish

  1. @ginerjm I think that's the problem; I'm trying to treat them all the same when they probably should be a little more unique. They slightly vary which is throwing me.

    {% snippet 'hello' %} to be <x-snippet name="hello" /> with a self closing tag

    {% extend 'boom' %} to be <x-template name="boom"> without a self closing tag

    {% endextend %} with the self closing tag </x-template>

  2. @Barand Sorry to bother you.

    Everything was going great until I came across the last couple tags I needed to replace which were not self closing like the others (example 4 and example 5 below) which caused issues.

    Can you think of a way around this problem? Or is there some regex solution which can pick out the first and last parts whilst skipping the word in the quotes? 🤢

    {% snippet name="hello1" %}
    // OK
    <x-snippet name="hello1" />
    
    {% setting name="hello2" %}
    // OK
    <x-setting name="hello2" />
    
    {% collection name="hello3" %}
    // OK
    <x-collection name="hello3" />
    
    {% extend "boom" %}
    // Issue with />
    <x-template name="boom">
      
    {% endextend %}
    // Issue with />
    <x-template>

    Code

    $source = "{% snippet name='hello1' %} {% setting name='hello2' %} {% collection name='hello3' %} {% extend 'boom' %} {% endextend %}";
    $html = str_replace([
        '{% snippet ', 
        '{% collection ', 
        '{% setting ', 
        '{% extend ', 
        '{% endextend ', 
        '%}'
    ], 
    [ 
        '<x-snippet name=',
        '<x-collection name=',
        '<x-setting name=',
        '<x-template name=',
        '</x-template ',
        '/>'
    ], 
    $source);
    dump($html);

    Result (see areas in red which are incorrect tags)

    image.png.b0861fbaf4c3e723532b6cfa582438a4.png

  3. @BarandThank you very much; that is exactly what I was looking for and works perfectly. Quick question:

    Is there a more elegant way to do multiple replacements? The below code does work; but looks a little unwealdy.

    $html = str_replace( ['{% snippet ', '%}'], ['<x-snippet name=', '/>'], $page->template->source);
    $html = str_replace( ['{% collection ', '%}'], ['<x-collection name=', '/>'], $html);
    $html = str_replace( ['{% setting ', '%}'], ['<x-setting name=', '/>'], $html);

     

  4. Hi,

    I have some html stored in a variable called $html

    Inside this $html variable I have the following content {% snippet "footer" %}

    I would like to find all instances of this tag in the $html and replace it with <x-snip name="footer" />

    Could anyone advise me on how this would be best achieved?

    Thank you,

    MoFish

    --

    Ideally this would work for any number of these custom tags e.g.

    {% snippet "footer" %} and {% snippet "header" %} would become <x-snip name="footer" /> and <x-snip name="header" />

  5. Hi,

    I have a textarea called registerDomain which i enter multiple domain names into (separated by a new line) e.g.

    • www.google.com
    • www.sky.com
    • www.bt.com

    I have the following code to get each of the domains and format them like

    • google.com (lowercase without any spaces)
    • sky.com (lowercase without any spaces)
    • bt.com (lowercase without any spaces)

    The below code I have works perfectly, however its awful to look at. I wondered if anyone could advise on a cleaner/more elegant way of achieving this.

    Thanks,

    	if (isset($_POST['submit'])) {
    		$values = trim($_POST['registerDomain']);
    		$array = explode("\n", $values);
    		$array = array_filter($array, 'trim');
    		
    		foreach ($array as $line) {
    			$domain = addslashes($line);
    			$domain = preg_replace("/\r|\n/", "", $domain);
    			$domain = strtolower($domain);
    			$domain = str_replace("www.", "", $domain);
    			$domain = str_replace(" ", "", $domain);

     

  6. @NotionCommotion

    You are correct, some nulls are coming back but i don't understand why there would be no response message with them.

    $textAr: ["www.google.com\r","www.bbc.com\r","www.sky.com"]
    $line: google.com bool(false)
    $line: bbc.com bool(false)
    $line: sky.com string(203) " status error error Domain sky.com already registered "
    array(3) { ["www.google.com "]=> bool(false) ["www.bbc.com "]=> bool(false) ["www.sky.com"]=> string(203) " status error error Domain sky.com already registered " }

  7. @NotionCommotion

    I was hoping for something like this, as each of these are already registered.

    Quote

    Array ( [www.google.com ] => status error error Domain google.com already registered [www.bbc.com ] => status error error Domain bbc.com already registered [www.sky.com] => status error error Domain sky.com already registered )

    However, only get the last response

    Quote

    $textAr: ["www.google.com\r","www.bbc.com\r","www.sky.com"]
    $line: google.com
    $line: bbc.com
    $line: sky.com status error error Domain sky.com already registered
    Array ( [www.google.com ] => [www.bbc.com ] => [www.sky.com] => status error error Domain sky.com already registered )

     

  8. $textAr: ["www.google.com\r","www.yahoo.com"]
    $line: www.google.com
    $line: www.yahoo.com status error error Domain yahoo.com already registered
    Array ( [0] => [1] => status error error Domain yahoo.com already registered )

    Its like the second request blanks out the first. If i do three requests, i only ever get the last one.

    $textAr: ["www.google.com\r","www.yahoo.com\r","www.bbc.com"]
    $line: www.google.com
    $line: www.yahoo.com
    $line: www.bbc.com status error error Domain bbc.com already registered
    Array ( [0] => [1] => [2] => status error error Domain bbc.com already registered )

  9. @NotionCommotion

    $line: www.xxx2.com $textAr: ["www.xxx1.com\r","www.xxx2.com"] Array ( [0] => [1] => status error error The domain xxx2.com already exists in our database. This may occur if there is a pending Order for xxx2.com in our database under your account or any other account. You may search for this domain within your control panel.)

     

    
    	$result = array();
    	
    	foreach ($textAr as $line) {
    				
    		$url = "https://example.com/api/domains/blah.xml?domain-name={$line}";
    		
    		$ch = curl_init();
    		curl_setopt($ch, CURLOPT_URL, $url);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
    		curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
    		$result[] = curl_exec ($ch);
    		curl_close ($ch);	
    		
    	}
    	
    	printf('$line: %s'.PHP_EOL, $line);
    	printf('$textAr: %s'.PHP_EOL, json_encode($textAr)); 
    	print_r($result);
    

     

  10. xxx1.com
    xxx2.com

    Array ( [0] => [1] => status error error The domain xxx2.com already exists in our database. This may occur if there is a pending Order for xxx2.com in our database under your account or any other account.)

    As you can see, its only getting the result from the last one for some reason.

  11. Hi @requinix

    Apologies i removed that URL bit to simplify it. The $line is passed into the url of the curl call so each is unique.

    When using the below code and calling 3 curl requests for example; it only gets the last string response even if i try to append.

    I cannot figure out whats going on!

    if (isset($_POST['submit'])) {
    
    	$result = array();
    
    	foreach ($textAr as $line) {
    		
    		$url = "https://xxx.com/api/domains/meh.xml?auth-userid=xxx&domain-name={$line}";
    
    		$ch = curl_init();
    		curl_setopt($ch, CURLOPT_URL, $url);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
    		curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
    		$result = curl_exec ($ch);
            // $result[] = curl_exec ($ch);
    		curl_close ($ch);
    		print_r($result);
    		
    	}
    
        // print_r($result);
    
    }

     

     

     

  12. Hi,

    It returns a string like the following:

    string(506) " status error error The domain xxx.co.uk already exists in our database. This may occur if there is a pending Order for xxx.co.uk in our database under your account or any other account."

    I would like to collate all these return strings for each of the curl request (could be 10+) and return them as an alert/echo at the end of them all.

    if (isset($_POST['submit'])) {
    
    	$result = array();
    
    	foreach ($textAr as $line) {
    		$ch = curl_init();
    		curl_setopt($ch, CURLOPT_URL, $url);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
    		curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
    		$result = curl_exec ($ch);
    		curl_close ($ch);
    		var_dump($result);
    		
    	}
    
    	// i would like all of the strings to be compiled into one for returning to the screen
    	// but it seems like it only gets the last one instead of them all, even if i try string appending.
    	// is there a way to wait until one is completed before the next?
    
    }

    Thank you for your help.

  13. Hello,

    I am trying to do multiple curl requests and get the response for each one output to the screen.

    I have wrote the following code, but only appear to be getting the last response when outputting $result.

    What am i doing wrong? Is there a way to wait for each curl request to be completed before appending to $result so I can see them all?

    Thanks very much

    if (isset($_POST['submit'])) {
    	$result = array();
    	foreach ($textAr as $line) {
    		$ch = curl_init();
    		curl_setopt($ch, CURLOPT_URL, $url);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
    		curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
    		$result[] = curl_exec ($ch);
    	}
    	curl_close ($ch);
    
    	echo "<pre>";
    	print_r($result);
    	echo "</pre>";
    
    }

     

  14. Hi,

     

    I am looking to alter this slightly and am not sure how best to go around it. What I am now trying to do is:

    • Set a start date e.g first day of January (2017-01-01)
    • Set a end date e.g last day of December (2017-12-31)
    • Find the number of weeks between the two date ranges (2017-01-01, 2017-12-31) which returns 52 weeks.
    • And the tricky bit - for each of these 52 weeks - find the weeks startdate (Monday this week) and enddate (Friday this week).

    I have got the first three steps working using the function below, but am a little unsure on how best to find out the start date and end dates of these particular 52 weeks.

     

    I am maybe over thinking it, but i cannot understand gow best to tackle the next steps - as am unsure on how to find out a single date in each week.

     

    Any help much appriciated.

     

    MoFish

    function datediffInWeeks($start, $end)
    {
        if($start > $end) return datediffInWeeks($end, $start);
        $sd = DateTime::createFromFormat('Y-m-d', $start);
        $ed = DateTime::createFromFormat('Y-m-d', $end);
        return floor($sd->diff($ed)->days/7);
    
        // foreach of these weeks
           // find the start date 'Monday this week'
           // find the end date 'Friday this week'
        // return an array
    }
    
    var_dump(datediffInWeeks('2017-01-01', '2017-12-31')); // 52
    
  15. Thanks, I think I have got the week calculation bit working after some messing around.

     

    I am trying to change my array slightly to 'group' the results into week numbers but am hitting some road blocks. I seem to get one week in the correct one, but not all of them

     

    I currently have:

    Array
    (
        [0] => stdClass Object
            (
                [SALE_ID] => 1
                [SALE_DATE] => 2017-01-01
                [SALE_WEEK] => 1
            )
    
        [1] => stdClass Object
            (
                [SALE_ID] => 1
                [SALE_DATE] => 2017-02-15
                [SALE_WEEK] => 4
            )
    
        [2] => stdClass Object
            (
                [SALE_ID] => 1
                [SALE_DATE] => 2017-03-25
                [SALE_WEEK] => 1
            )
    
        [3] => stdClass Object
            (
                [SALE_ID] => 1
                [SALE_DATE] => 2017-04-10
                [SALE_WEEK] => 3
            )
    
    )
    

    And would like to have something like the below - whereby I can loop out all the records based on the week.

    Array
    (
        [week1] => stdClass Object
        (
            [1] => stdClass Object
            (
                [SALE_ID] => 1
                [SALE_DATE] => 2017-01-01
                [SALE_WEEK] => 1
            )
            [2] => stdClass Object
            (
                [SALE_ID] => 1
                [SALE_DATE] => 2017-03-25
                [SALE_WEEK] => 1
            )
        )
        [week3] => stdClass Object
        (
            [1] => stdClass Object
            (
                [SALE_ID] => 1
                [SALE_DATE] => 2017-04-10
                [SALE_WEEK] => 3
            )
        )
        [week4] => stdClass Object
        (
            [1] => stdClass Object
            (
                [SALE_ID] => 1
                [SALE_DATE] => 2017-02-15
                [SALE_WEEK] => 4
            )
        )
    )
    

    At the moment I have:

    		$results = array(
    		  	array("SALE_ID"=>"1","SALE_DATE"=>"2017-01-01"),
    		  	array("SALE_ID"=>"1","SALE_DATE"=>"2017-02-15"),
    		  	array("SALE_ID"=>"1","SALE_DATE"=>"2017-03-25"),
    		  	array("SALE_ID"=>"1","SALE_DATE"=>"2017-04-10")
    		);
    		
    		$obj = json_decode (json_encode ($results), FALSE);
    		
    		foreach ($obj as $r){
    			$date = new DateTime($r->SALE_DATE);
    			$r->SALE_WEEK = $this->sale_model->week_calc($date);
    		}
    

    Thanks,

     

    MoFish

  16. Thanks Barand,

     

    I've not been able to use the function successfully due to the information I'm passing in being incorrect. I'm not sure how best to format this.

     

    DB value: // 2017-02-17 00:00:00

     

    Argument 1 passed to sale_model::week_number() must be an instance of DateTime

  17. Hi Requinix,

     

    Thanks for taking the time to respond.

     

    I have tried your example - but am getting the following when trying it with my code from value from the DB.

     

    Argument 1 passed to sale_model::week_number() must be an instance of DateTime

     

    The value is 2017-02-17 00:00:00 from the database - should this be amended before going into the function?

     

    Thanks,

     

    MoFish

    // return
    $return = $this->query("select * from SALE;", true);
    		
    // foreach
    foreach ($return as $item){
        echo $item->SALE_DATE; //2017-02-17 00:00:00 
        $week = $this->week_number($item->SALE_DATE);
        $item->week = $week;
    }
    
    // print_r ($item);
    
  18. Hi,

     

    I'm trying to associate a week number for a date I receive from my database.

     

    The week number is calculated between the last Friday of the previous month and the last Friday of the current month for that particular date. I would expect the week number to be 1,2,3 or in some cases maybe a 4 week month.

     

    I tried to use DatePeroid without much success, I've never used it before but feel im using it incorrectly as something is not quite right.

     

    Could anyone help?

     

    Regards,

     

    MoFish

    // try to find the week number
    foreach(new DatePeriod($rstart, new DateInterval('P1D'), $rend) as $date) {
       $item->SALE_WEEK_NUMBER = "Week".$date->format('W');   
    }  
    

    The code below calculates the date ranges from the database date.

            // foreach
            foreach ($return as $item){
                                
                // month range start
                $rs = new DateTime($item->SALE_DATE, $time_zone);
                $rstart = $rs->modify('last friday of previous month');
                echo "<br/><br/> Range Start: " . $rs->format('Y-m-d');
                
                // month range date
                $re = new DateTime($item->SALE_DATE, $time_zone);
                $rend = $re->modify('last friday of this month');
                echo "<br/> Range End: " . $re->format('Y-m-d');
    
                // try to find the week number
                foreach(new DatePeriod($rstart, new DateInterval('P1D'), $rend) as $date) {
                    $item->SALE_WEEK_NUMBER = "Week".$date->format('W');    
                }            
                
            }
    
  19. Hi Barand,

     

    Thanks for this. This was not producing the results as I was expecting - however I have modified the query and this seems to be working now.

    SELECT * FROM TABLE WHERE `DDATE` BETWEEN (UTC_DATE() - INTERVAL 2 DAY) AND UTC_DATE()
    

    Thanks everyone for taking the time to respond.

     

    MoFish

  20. Hi requinex,

     

    Yes, I want all of the results. I have tried your query - however this is not working quite as expected. Maybe this will help explain what I'm trying to do:

    if TODAY is in the 3 DAY RANGE of DDATE value - then display results
    
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.