Asked  2 Years ago    Answers:  5   Viewed   76 times

Consider:

$xml = "l";
$xml = "vv";

echo $xml;

This will echo vv. Why and how can I do multi-line strings for things like SimpleXML, etc.?

 Answers

3

Well,

$xml = "l
vv";

Works.

You can also use the following:

$xml = "lnvv";

or

$xml = <<<XML
l
vv
XML;

Edit based on comment:

You can concatenate strings using the .= operator.

$str = "Hello";
$str .= " World";
echo $str; //Will echo out "Hello World";
Thursday, August 25, 2022
4

A regex would be simplest:

$input = 'foo_left.jpg';
if(!preg_match('/_(left|right|center)/', $input, $matches)) {
    // no match
}

$pos = $matches[0]; // "_left", "_right" or "_center"

See it in action.

Update:

For a more defensive-minded approach (if there might be multiple instances of "_left" and friends in the filename), you can consider adding to the regex.

This will match only if the l/r/c is followed by a dot:

preg_match('/(_(left|right|center))./', $input, $matches);

This will match only if the l/r/c is followed by the last dot in the filename (which practically means that the base name ends with the l/r/c specification):

preg_match('/(_(left|right|center))\.[^\.]*$/', $input, $matches);

And so on.

If using these regexes, you will find the result in $matches[1] instead of $matches[0].

Saturday, August 27, 2022
2

This can't work properly. Stored with Unicode there are many more Characters than with ANSI. So if you "convert" to ANSI, you will loose lots of charackters.

http://php.net/manual/en/function.htmlentities.php

You can use Unicode (UTF-8) charset with htmlentities:

string htmlentities ( string $string [, int $flags = ENT_COMPAT [, string $charset [, bool $double_encode = true ]]] )

htmlentities($myString, ENT_COMPAT, "UTF-8"); should work.

Saturday, October 15, 2022
 
zbr
 
zbr
3

I agree with Aaron about the desirability of your stylistic choice, but since I also agree with him that Emacs Lisp is fun, I'll describe how you might go about implementing this.

Emacs python-mode computes the indentation of a line in the function python-calculate-indentation and the relevant section for handling continuation lines is buried deep inside the function, with no easy way to configure it.

So we have two options:

  1. Replace the whole of python-calculate-indentation with our own version (a maintenance nightmare whenever python-mode changes); or
  2. "Advise" the function python-calculate-indentation: that is, wrap it in our own function that handles the case we're interested in, and otherwise defers to the original.

Option (2) seems just about doable in this case. So let's go for it! The first thing to do is to read the manual on advice which suggests that our advice should look like this:

(defadvice python-calculate-indentation (around continuation-with-dot)
  "Handle continuation lines that start with a dot and try to
line them up with a dot in the line they continue from."
  (unless 
      (this-line-is-a-dotted-continuation-line) ; (TODO)
    ad-do-it))

Here ad-do-it is a magic token that defadvice substitutes with the original function. Coming from a Python background you might well ask, "why not do this decorator-style?" The Emacs advice mechanism is designed (1) to keep advice well separated from the original; and (2) to have multiple pieces of advice for a single function that don't need to co-operate; (3) to allow you individual control over which pieces of advice are turned on and off. You could certainly imagine writing something similar in Python.

Here's how to tell if the current line is a dotted continuation line:

(beginning-of-line)
(when (and (python-continuation-line-p)
           (looking-at "\s-*\."))
    ;; Yup, it's a dotted continuation line. (TODO)
    ...)

There's one problem with this: that call to beginning-of-line actually moves point to the beginning of the line. Oops. We don't want to move point around when merely calculating indention. So we better wrap this up in a call to save-excursion to make sure that point doesn't go a-wandering.

We can find the dot that we need to line up with by skipping backwards over tokens or parenthesized expressions (what Lisp calls "S-expressions" or "sexps") until either we find the dot, or else we get to the start of the statement. A good Emacs idiom for doing a search in a restricted part of the buffer is to narrow the buffer to contain just the part we want:

(narrow-to-region (point)
                  (save-excursion
                    (end-of-line -1)
                    (python-beginning-of-statement)
                    (point)))

and then keep skipping sexps backwards until we find the dot, or until backward-sexp stops making progress:

(let ((p -1))
  (while (/= p (point))
    (setq p (point))
    (when (looking-back "\.")
      ;; Found the dot to line up with.
      (setq ad-return-value (1- (current-column)))
      ;; Stop searching backward and report success (TODO)
      ...)
    (backward-sexp)))

Here ad-return-value is a magic variable that defadvice uses for the return value from the advised function. Ugly but practical.

Now there are two problems with this. The first is that backward-sexp can signal an error in certain circumstances, so we better catch that error:

(ignore-errors (backward-sexp))

The other problem is that of breaking out of the loop and also indicating success. We can do both at once by declaring a named block and then calling return-from. Blocks and exits are Common Lisp features so we'll need to (require 'cl)

Let's put it all together:

(require 'cl)

(defadvice python-calculate-indentation (around continuation-with-dot)
  "Handle continuation lines that start with a dot and try to
line them up with a dot in the line they continue from."
  (unless 
      (block 'found-dot
        (save-excursion
          (beginning-of-line)
          (when (and (python-continuation-line-p)
                     (looking-at "\s-*\."))
            (save-restriction
              ;; Handle dotted continuation line.
              (narrow-to-region (point)
                                (save-excursion
                                  (end-of-line -1)
                                  (python-beginning-of-statement)
                                  (point)))
              ;; Move backwards until we find a dot or can't move backwards
              ;; any more (e.g. because we hit a containing bracket)
              (let ((p -1))
                (while (/= p (point))
                  (setq p (point))
                  (when (looking-back "\.")
                    (setq ad-return-value (1- (current-column)))
                    (return-from 'found-dot t))
                  (ignore-errors (backward-sexp))))))))
    ;; Use original indentation.
    ad-do-it))

(ad-activate 'python-calculate-indentation)

I won't claim that this is the best way to do this, but it illustrates a bunch of moderately tricky Emacs and Lisp features: advice, excursions, narrowing, moving over sexps, error handling, blocks and exits. Enjoy!

Saturday, December 17, 2022
3

You have to replace the TPasswordEdit with TNewMemo:

var
  JsonMemo: TNewMemo;

procedure InitializeWizard();
var
  ContractConfigPage: TInputQueryWizardPage;
  JsonIndex: Integer;
  JsonEdit: TCustomEdit;
begin
  { Create new page }
  ContractConfigPage := CreateInputQueryPage(wpWelcome,
    'Map contract as JSON', 'Please enter the map contract to use in JSON format', '');    

  { Add TPasswordEdit. We use it only to have Inno Setup create the prompt label and }
  { to calculate the proper location of the edit control }
  JsonIndex := ContractConfigPage.Add('JSON', False);
  JsonEdit := ContractConfigPage.Edits[JsonIndex];

  { Create TNewMemo (multi line edit) on the same parent control and }
  { the same location (except for height) as the original single-line TPasswordEdit }
  JsonMemo := TNewMemo.Create(WizardForm);
  JsonMemo.Parent := JsonEdit.Parent;
  JsonMemo.SetBounds(JsonEdit.Left, JsonEdit.Top, JsonEdit.Width, ScaleY(100));

  { Hide the original single-line edit }
  JsonEdit.Visible := False;

  { Link the label to the new edit }
  { (has a practical effect only if there were a keyboard accelerator on the label) }
  ContractConfigPage.PromptLabels[JsonIndex].FocusControl := JsonMemo;
end;

Now you cannot use ContractConfigPage.Edits to access the TNewMemo and its value (it references the original [hidden] TPasswordEdit). You have to use the global JsonMemo variable.


You can of course create the page completely yourself, starting from a clean page using CreateCustomPage. It might be a cleaner solution, but more laborious.

Tuesday, November 15, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages