I have a string with multiple lines. For this example, my string will be this:
Name:Jaxo
Description:A person on Stackoverflow
Question:$this->questionName();
How could I get just, say, the 'Description'? I need everything after the description, ie 'A person on Stackoverflow'. I've tried a regex like this, but it doesn't work: /^Description:(.+?)\n/i
Any help is much appreciated!
Thanks
-Jaxo
This should work for you:
if (preg_match('/Description:(.+)/im', $subject, $regs)) {
$result = $regs[1];
} else {
$result = "";
}
Where $result is the Description name.
If there is a newline character separating each part of the label you could explode.
$array = explode("\n",$string); // separate params
$desc = explode(":",$array[1]); // separate description
This way you could get any of the parameters.
Try this:
$a="Name:Jaxo
Description:A person on Stackoverflow
Question:\$this->questionName();";
preg_match("/Description:([^\n]+)/i",$a,$m);
print_r($m);
Output:
Array ( [0] => Description:A person on Stackoverflow [1] => A person on Stackoverflow )
Related
I have email format like
iba#test.com
Expected output
#test.com
I need to remove the name after the mail id how can I do that I have tried str_replace but that does not give me a solution.
Please try like this :
let string = "iba#test.com"
const result = `#${string.split('#')[1]}`
using explode
$email= "iba#test.com"
$domain= explode("#",$email);
echo $domain[0]; // iba
echo $domain[1]; //test.com
I've had a look at various StackOverflow questions but still can't seem to find a successful way to use a variable inside a shortcode. The code in question is as follows (the variable I'm trying to insert into the shortcode is $track_audio.
<?php
$track_audio = get_sub_field('mp3_url');
$renderedShortcode = do_shortcode('[fusion_audio src=". $track_audio . loop="off"][/fusion_audio]');
echo $renderedShortcode; ?>
I've read a fair few conversations on Stackoverflow but have not found a solution that works. The last idea i tried was to concatenate the variable (see code) but that hasn't worked either. Is anyone able to tell me the correct way to do this?
Many thanks in advance for your help with this
Phil
Try this way working for me
Change code from
$renderedShortcode = do_shortcode('[fusion_audio src=". $track_audio . loop="off"][/fusion_audio]');
TO
$renderedShortcode = do_shortcode('[fusion_audio src="'.$track_audio.'" loop="off"][/fusion_audio]');
Final conclision:
$track_audio = get_sub_field('mp3_url');
$renderedShortcode = do_shortcode('[fusion_audio src="'.$track_audio.'" loop="off"][/fusion_audio]');
echo $renderedShortcode;
function fusion_audio_shortcode_func( $atts, $content = null ) {
extract( shortcode_atts(
array(
'loop' => '',
), $atts )
);
$loop;
return '<awesomeness>' . $content . '</awesomeness>';
}
add_shortcode( 'fusion_audio', 'fusion_audio_shortcode_func' );
I use PHP to process following input:
sam
99912222
tom
11122222
harry
12299933
sam
edward
harry
the 1st to 6th line are name and phone numbe. And the last three lines is the search query, if the name is not in the list(not have phone number,print not found), otherwise output the data. My code as follow:
<?php
$_fp = fopen("php://stdin", "r");
$list = array();
for($i = 0;$i<3;$i++){
$name = strtolower(fgets($_fp));
$phone = fgets($_fp);
$list["$name"] = $phone;
}
for($i = 0;$i<3;$i++){
$name = fgets($_fp);
if(array_key_exists($name,$list)){
echo "$name".'='."$list[$name]"."\n";
}else{
echo 'Not found'."\n";
}
?>
Excepted output should be sam = 99912222 Not found harry = 12299933
The output is sam = 99912222 Not found Not found. why these function doesn't work?
This is a problem from hackerrank.
I know if I use hashmap in java is easy to solve. But how can I solve this problem in PHP?
Many thanks
First, trim off whitespace by using trim(fgets($_fp)) everywhere instead of just fgets($_fp) -- that fixes things on my end at least.
Second, the code you pasted is missing the closing curly bracket on your second for loop.
Third, have fun with 30 Days of Code :-) (once you get the above straightened out you also need to have your code read in the number of entries at the beginning, and "Read the queries until end-of-file" at the end).
I have a string ="/show/search/All.aspx?Att=A1". How to get the last value after the 'Att=' in efficient way ?
You could do a split on the '=' character.
Example (in C#):
string line = "/show/search/All.aspx?Att=A1";
string[] parts = line.Split('=');
//parts[1] contains A1;
Hope this helps
If you're only dealing with this one URL then both of the other answers would work fine. I would consider using the HttpUtility.ParseQueryString method and just pull out the item you want by key.
Whatever an
efficient way
is...
Try this:
var str = "/show/search/All.aspx?Att=A1";
var searchString = "Att=";
var answer = str.Substring(str.IndexOf(searchString) + searchString.Length);
Do you know of any way to reference an object in the replacement part of preg_replace. I'm trying to replace placeholders (delimited with precentage signs) in a string with the values of attributes of an object. This will be executed in the object itself, so I tried all kinds of ways to refer to $this with the /e modifier. Something like this:
/* for instance, I'm trying to replace
* %firstName% with $this->firstName
* %lastName% with $this->lastName
* etc..
*/
$result = preg_replace( '~(%(.*?)%)~e', "${'this}->{'\\2'}", $template );
I can't get any variation on this theme to work. One of the messages I've been getting is: Can't convert object Model_User to string.
But of course, it's not my intention to convert the object represented by $this to a string... I want to grab the attribute of the object that matches the placeholder (without the percentage signs of course).
I think I'm on the right track with the /e modifier. But not entirely sure about this either. Maybe this can be achieved much more simple?
Any ideas about this? Thank you in advance.
Like I commented to Paul's answer: in the meanwhile I found the solution myself. The solution is much more simple than I thought. I shouldn't have used double quotes.
The solution is as simple as this:
$result = preg_replace( '~(%(.*?)%)~e', '$this->\\2', $template );
Hope this helps anyone else for future reference.
Cheers.
Check out preg_replace_callback - here's how you might use it.
class YourObject
{
...
//add a method like this to your class to act as a callback
//for preg_replace_callback...
function doReplace($matches)
{
return $this->{$matches[2]};
}
}
//here's how you might use it
$result = preg_replace_callback(
'~(%(.*?)%)~e',
array($yourObj, "doReplace"),
$template);
Alternatively, using the /e modifier, you could maybe try this. I think the only way to make it work for your case would be to put your object into global scope
$GLOBALS['yourObj']=$this;
$result = preg_replace( '~(%(.*?)%)~e', "\$GLOBALS['yourObj']->\\2", $template );