I have the following string generating mp3 urls for a music player on my site.
<?php echo $song->getTitle() ?>
which results in /public/music_song/df/74/746b_2112.mp3?c=ec1e
I would like to remove the query from the string
resulting in /public/music_song/df/74/746b_2112.mp3
I've looked into how to split the url, but I'm nowhere near being a php genius just yet so I dont know weather to split or use preg_replace or how the heck to incorporate it into my existing string.
I have to get rid of these queries, they are unneeded and crashing my databases on a daily basis.
list($keep) = explode('?', '/public/music_song/df/74/746b_2112.mp3?c=ec1e');
$parsedInput = parse_url('/public/music_song/df/74/746b_2112.mp3?c=ec1e');
echo $parsedInput['path'];
// Results in /public/music_song/df/74/746b_2112.mp3
Edit: Since I havent worked with SocialEngine, Im guessing that what you need to do is:
<?php $parsed = parse_url($song->getFilePath());
echo $this->htmlLink($parsed['path'],
$this->string()->truncate($song->getTitle(), 50),
array('class' => 'music_player_tracks_url',
'type' => 'audio',
'rel' => $song->song_id )); ?>
Related
This question already has answers here:
PHP - how to create a newline character?
(15 answers)
Closed 4 years ago.
I have an index.php file and an array which has message. Is there a way that instead of <br> tag I can display the text with a new line in PHP so I can also store it in database?
The code:
$array = array(
array(
'id' => 1,
'message' => 'I\'m reading Harry Potter!',
),
array(
'id' => 2,
'message' => 'Ok. I just got a notification that you sent me a pin on Pinterest.<br>Will you come to school tomorrow?',
)
);
For example:
Ok. I just got a notification that you sent me a pin on Pinterest.
Will you come to school tomorrow?
The new line character is \n. Simply replace <br> with \n and you will have the results you're looking for.
PHP - how to create a newline character?
Note!
php does not process escape characters within single quotes.
'\n' is not processed as a new line character, while "\n" is.
What is the difference between single-quoted and double-quoted strings in PHP?
Depending on your platform, you may want to be more specific about which new line sequence you choose.
\r\n, \r and \n what is the difference between them?
$array = array(
array(
'id' => 1,
'message' => "I'm reading Harry Potter!"
),
array(
'id' => 2,
'message' => "Ok. I just got a notification that you sent me a pin on Pinterest.\nWill you come to school tomorrow?"
)
);
echo "<pre>";
print_r($array);
exit;
This will show you the raw formatting of your string.
To then convert new lines to the <br> tag for display on a webpage, you would pass that string to nl2br()
<?php echo nl2br($array[1]['message']); ?>
Is there a way that instead of <br> tag I can display the text with a new line in PHP
Yes you can easily do this with CSS
white-space: pre;
https://www.w3schools.com/cssref/pr_text_white-space.asp
Back in the day I used to do the whole "replace" thing, then I got bored of it. Now I just use CSS.
The pre option/setting will preserve whitespace much like using the <pre> tag. The only thing you have to watch for is indenting in the source code
<p style="white-space:pre;">
<?php echo $something; ?>
</p>
This extra space in the code will be added to the PHP output, instead do this:
<p style="white-space:pre;"><?php echo $something; ?></p>
You can close the php tag and reopen after the break.
For example -
'message'=>'Ok.I just got a notification that you sent me a pin on Pinterest. ?>
<br>
<?php Will you come to school tomorrow?',
Hello everyone im trying to retrieve data from a form with a POST request.
This data is posted into another website.
On the website where the data is created i have a text field called website. The data filled in this field goes to the other website where the data is collected. Now i want to exclude the 'www' part. for example if the user enters www.hello.nl i want to receive hello.nl only.
What i tried:
function website () {
$str = $_POST['billing_myfield12'];
echo chop($str,"www");
}
// end remove www
// prepare the sales payload
$sales_payload = array(
'organization_id' => $organization_id,
'contact_id' => $contact_id,
'status' => 'Open',
'subject' => $_product->post_title." ".website(), <----- here i call it
This is not working. Is there a way to do this?
You can use trim() or specifically ltrim() to trim way the www. on the left side. Please don't forget the . after www.
echo ltrim($str, "www.");
Sample Code
echo ltrim("www.hello.nl", "www."); // hello.nl
Demo: http://ideone.com/bqMY7X
Looks like there are side effects with the above code. Let's go with the traditional str_replace method:
echo str_replace("www.", "", $str);
Also, we are sure that it should replace only from the first characters. So, we need to use a preg_replace instead, making it replace from the start.
echo preg_replace("/^www\./g", "", $str);
Verified the above code with: https://regex101.com/r/dv8N6d/1
I have some php file and it contains some comments.
<?php
$test = array(
'LBL_TEXT' => 'text',//test comment
'LBL_FOO' => 'foo'
);
Now I need to update 'LBL_TEXT' value(text) above file without removing comment('//test comment'). Is it possible using fwrite() or some other way.
So you will need something like
<?php
$data = file_get_contents("foo.php");
$data = so something clever to automatically change string as desired;
file_put_contents("foo.php",$data);
?>
I have an XML file that contains FAQs, but some of the contents of the answers need to use PHP functions to output appropriate content.
How can I find the placeholders within the answers, and replace them with PHP functions? Ideally, I would like to be able to have the functions set as variables to be changeable across multiple websites this code would be used on.
XML File (placeholders in last block, %LOCAL_NO% and %PREM_NO%)
<?xml version="1.0" encoding="UTF-8"?>
<faqs>
<faq>
<category>General</category>
<q>How old do I have to be to use your service?</q>
<a>You must be at least 18 years of age.</a>
</faq>
<faq>
<category>General</category>
<q>How long is a psychic reading?</q>
<a>The length of a psychic reading is completely up to you. It depends on the number and complexity of the questions you ask. The average length of a reading is 15 to 20 minutes.</a>
</faq>
<faq>
<category>General</category>
<q>Can I choose the psychic I speak with?</q>
<a>Of course! You can choose who you would like to speak to by selecting your desired psychic's profile and following the online prompts via the online booking page, or call us on %PREM_NO% and enter their PIN, or call %LOCAL_NO% and our live receptionists will connect you to a psychic that matches your requirements!</a>
</faq>
</faqs>
PHP output
<?php // General FAQs
$faqGeneral = $xml->xpath("/faqs/faq[category='General']");
echo "<h2>General</h2>";
foreach ($faqGeneral as $faq) { ?>
<h3><?php echo $faq->q; ?></h3>
<p><?php echo $faq->a; ?></p>
<?php } ?>
That looks like a case for preg_replace_callback, of course called before evaluating the XML. Which also ensures that the "PHP-echoed" values do not break XML syntax.
$data = array(
'tags' => array(
'PREM_NO' => '1-800-CSICOP',
)
);
$filledXML = preg_replace_callback(
'#%(\\w+)%#', // A-Z and underscore between %%'s
function ($matches) use ($data) {
switch($matches[1]) {
case 'PREM_NO':
case 'WHATEVER':
return $data['tags'][$matches[1]];
case 'YYYYMMDD':
return date('Y-m-d');
default:
return '';
}
},
$xmlString);
// $xml = loadXML($xmlString);
$xml = loadXML($filledXML);
This allows for special tags such as YYYYMMDD to return runtime-calculated values, as well as externals. You can even include a PDO handle in $data and be able to run SQL queries inside the function.
A simpler version
$tags = array(
'%PREM_NO%' => '12345',
'%YYYYMMDD%' => date('Y-m-d'),
// ... et cetera
);
$filledXML = str_replace(array_keys($tags), array_values($tags), $xmlString);
If you know the strings to match and the values before (i.e., not dynamic) then you can just do a str_replace inline.
If they are dynamic then you can grab the values from your database (or wherever you are storing them) and then loop through and str_replace them.
Or, you could use regex, something like /(\%[a-z_]+\%)/i. For that you can look into preg_match_all.
str_replace in the PHP manual
preg_match_all in the PHP manual
Update: You can use arrays as parametera for str_replace. E.g.,
$find = array('%PREM_NO%', '%LOCAL_NO%');
$replace = array('012345', '67890');
$answer = str_replace($find, $replace, $faq->a);
I created this variable to store this string:
$extension = '/index.php?Ah83kL80='.$id;
And I'm trying to add the $extension to this link:
<a href="'.Yii::app()->createUrl('image/index',array('album'=>$album->content)).'">
So far, I tried doing this:
<a href="'.Yii::app()->createUrl('image/index',array('album'=>$album->content, 'index' => $extension)).'">
But it put some sort of other characters that I never intended to include.
Output:
/index/%2Findex.php%3FAh83kL80%3D
According to the documentation I found, the second param is an array of URL parameters. That means that you don't want a string like '?Ah83kL80='.$id but want to pass the Ah83kL80 key and $id value separately, as you're already doing with album and $album->content.
I'm not familiar with Yii but try building your link like this... I'm going to format this differently than you have so I can indent and make it easier to read.
$link = Yii::app()->createUrl(
'image/index',
array(
'album'=>$album->content,
'Ah83kL80' => $id,
),
);